qt mysql导出csv,将.csv文件导入到QT gui上的sqlite3数据库表

I am writing a QT GUI application that will import a .csv file to a sqlite database table

my .csv file is in the path /home/aj/import_table.csv

and my database is in /home/aj/testdatabase.db

i wrote the below code block---

void MainWindow::on_importButton_clicked()

{

QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");

db.setDatabaseName("/home/aj/testdatabase.db");

QString querystr;

querystr=QString(".separator ","");

QSqlQuery query(querystr,db);

if(query.exec())

{

qDebug()<

QString querystr2;

querystr=QString(".import import_table.csv test_table");

}

else

{

qDebug()<

}

}

but it is throwing error at compile time--

/home/aj/sqlite3test/mainwindow.cpp:34: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]

/home/aj/sqlite3test/mainwindow.cpp:34: error: conversion from ‘const char [1]’ to ‘QChar’ is ambiguous

/home/aj/sqlite3test/mainwindow.cpp:34: error: conversion from ‘const char [1]’ to ‘QChar’ is ambiguous

/usr/local/Trolltech/Qt-4.8.4/include/QtCore/qstring.h:90: error: initializing argument 1 of ‘QString::QString(int, QChar)’ [-fpermissive]

any solutions ???

is it happening because .separator and .import are sqlite terminal command and cannot be executed via the querystr=Qstring("... ... ..."); format ???

解决方案

This line:

querystr=QString(".separator ","");

is trying to construct a QString using two const char * as arguments to the constructor. According to the documentation there is no constructor that accepts this combination.

I think you meant to include the (") quotes inside the string. You need to escape them with backslashes, like this:

querystr=QString(".separator \",\"");

so that the compiler knows that they should be part of the string, rather than delimiting it.

That should fix your compilation error, HOWEVER your last comment is correct, the SQLite documentation states:

And, of course, it is important to remember that the dot-commands are

interpreted by the sqlite3.exe command-line program, not by SQLite

itself. So none of the dot-commands will work as an argument to SQLite

interfaces like sqlite3_prepare() or sqlite3_exec().

In other words, if you want to read in a CSV file from a C++ program you have written yourself, you will have to parse the file in your own code and insert the records via SQL INSERT commands.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值