Import CSV file to sqlite3 database table in QT gui
I am writing a QT GUI application that will import a CSV file into 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 following block of code ---
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();
}
}
but this is a compile error -
/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] in 'QChar ambiguous
/home/aj/sqlite3test/mainwindow.cpp:34: error: conversion from' const char [1] to 'QChar ambiguous
/usr/local/Trolltech/Qt-4.8.4/include/QtCore/ qstring.h: 90: error: initializing argument 1 from 'QString :: QString (int, QChar) [-fpermissive]
any solutions ???
this is because .separator and .import are sqlite terminal commands and cannot be executed via querystr = Qstring ("........."); format
source to share
This line:
querystr=QString(".separator ","");
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 wanted to include quotes (") inside the string. You need to escape them with backslashes, for example:
querystr=QString(".separator \",\"");
so that the compiler knows they should be part of the string and not constrain it.
This should fix your compilation error, HOWEVER , your last comment is correct, the SQLite documentation says:
And, of course, it's important to remember that dot commands are interpreted by the sqlite3.exe command line program, not SQLite itself. Therefore, none of the dot commands will work as an argument to SQLite interfaces such as sqlite3_prepare () or sqlite3_exec ().
In other words, if you want to read a C ++ program you created yourself into a CSV file, you will have to parse the file in your own code and insert the records via SQL INSERT commands.
source to share