将.csv文件导入到QT上的sqlite3数据库表gui
我正在写一个QT GUI应用程序,它将一个.csv文件导入到sqlite数据库表 我的.csv文件位于路径/home/aj/import_table.csv 和我的数据库是/home/aj/testdatabase.db将.csv文件导入到QT上的sqlite3数据库表gui
我写了下面的代码块---
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()<<"SUCCESSFULLY QUEIRED ";
QString querystr2;
querystr=QString(".import import_table.csv test_table");
}
else
{
qDebug()<<"ERROR DURING QUERY "<<db.lastError().text();
}
}
,但它是在编译时间 -
抛出错误/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]
任何解决方案?
是否发生这种情况,因为.separator和.import是sqlite终端命令,无法通过querystr = Qstring(“... ... ...”)执行;格式?
这条线:
querystr=QString(".separator ","");
试图构造使用两个const char *
作为参数传递给构造函数的QString。根据the documentation没有构造函数接受这个组合。 。
我想你的意思是这样的字符串内的引号(“)你需要用反斜杠逃避他们,就像这样:
querystr=QString(".separator \",\"");
让编译器知道他们应该是字符串的一部分,而不是界定其
这应该解决您的编译错误,无论其你最后的评论是正确的,SQLite documentation状态:
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().
换句话说,如果您想从自己编写的C++程序中读取CSV文件,则必须在自己的代码中解析该文件,并通过SQL INSERT命令插入记录。
非常感谢您确认我对.dot命令的怀疑。 编译错误现在被删除,但它仍然不起作用。 任何教程链接/文件解析.dot命令到我的c + +程序??? – RicoRicochet 2014-12-08 05:42:05
@AmarjitBiswas正如答案中所述,您不能直接从C++程序调用.dot命令。你可以使用例如。 popen运行sqlite cli工具并使用它来进行导入,但我不会推荐。我建议编写自己的简单解析和插入例程。 – harmic 2014-12-08 06:01:09
“但它是在运行时抛出错误” - 那些看起来像编译错误给我。 – harmic 2014-12-08 04:49:13
哪个是该函数中的第34行? – harmic 2014-12-08 04:49:28
对不起,编译时间.. – RicoRicochet 2014-12-08 04:52:06