我正在编写一个 QT GUI 应用程序,它将把 .csv 文件导入到 sqlite 数据库表中 我的 .csv 文件位于路径/home/aj/import_table.csv 我的数据库位于/home/aj/testdatabase.db
我写了下面的代码块---
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 命令插入记录。
关于c++ - 在QT gui上将.csv文件导入到sqlite3数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27351546/