<SOLVED> Qt Ubuntu SqLite driver not loaded
-
Hi everyone!
Im running Qt Creator 2.8.1 based on Qt 5.1.1 (GCC 4.6.1, 64 bit) on Ubuntu.Im trying to make a simple application using SQLite, but i cant get the driver loading.
On the .pro file I included QT += sql
Then my code is as follows:
@
#include <QtSql>
#include <QSqlDriver>
#include <QMessageBox>QMessageBox Error;
QSqlDatabase db;
QSqlQuery Query(db);
db= QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("example.db3");
if(!db.open())
{
Error.setText("Couldnt open databse");
Error.exec();
}
if(!db.isDriverAvailable("QSQLITE"))
{
Error.setText("Driver not available");
Error.exec();
}Query.prepare("CREATE TABLE t1 (t1key INTEGER PRIMARY KEY, data TEXT, num double, timeEnter Date)");
if(!Query.exec())
{
SqlError=Query.lastError();
Error.setText("Query error 001: "+SqlError.text());
Error.exec();
}@
When I run this, I get the error "Query error 001: Driver not loaded Driver not loaded"
Apparently the driver is available (the error "driver not available" doesnt show), and the database is opened, but i cant run any query.
I checked, and the database example.db3 is created on my program directory, so SQLite is working...
To get more information I added:@
QStringList list=db.drivers();
@
And when I debug this, list contains
"QSQLITE"
"QMYSQL"
"QMYSQL3"
"QPSQL"
"QPSQL7"
So the driver is deffinitly there, but it cant be loaded.
I read loads of forum posts with people having the same problem, but all of them were fixed when they moved into the program directory the sqldrivers folder. Im running my program from QT, and it doesnt work, so i guess that solution wont help me.
Any ideas with what im doing wrong? -
Hi,
Run ldd on your plugin to check whether it can find all the required libraries
-
Im kind of new to Linux, but I searched the libqsqlite.so located at (/Qt5.1.1/Tools/QtCreator/bin/plugins/sqldrivers) and ran the ldd on it, this was the result
nicolas@Ksi:~/Qt5.1.1/Tools/QtCreator/bin/plugins/sqldrivers$ ldd libqsqlite.so
linux-vdso.so.1 => (0x00007fff585fe000)
libQt5Sql.so.5 => /home/nicolas/Qt5.1.1/Tools/QtCreator/bin/plugins/sqldrivers/./../../../lib/qtcreator-/libQt5Sql.so.5 (0x00007fc5d6911000)
libQt5Core.so.5 => /home/nicolas/Qt5.1.1/Tools/QtCreator/bin/plugins/sqldrivers/./../../../lib/qtcreator/libQt5Core.so.5 (0x00007fc5d6286000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc5d6057000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fc5d5d57000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc5d5997000)
libicui18n.so.51 => /home/nicolas/Qt5.1.1/Tools/QtCreator/bin/plugins/sqldrivers/./../../../lib/qtcreator/./libicui18n.so.51 (0x00007fc5d557e000)
libicuuc.so.51 => /home/nicolas/Qt5.1.1/Tools/QtCreator/bin/plugins/sqldrivers/./../../../lib/qtcreator/./libicuuc.so.51 (0x00007fc5d51f7000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc5d4ff3000)
libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007fc5d4df1000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fc5d4be8000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007fc5d48f3000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fc5d45f7000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fc5d43e0000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc5d6e06000)
libicudata.so.51 => /home/nicolas/Qt5.1.1/Tools/QtCreator/bin/plugins/sqldrivers/./../../../lib/qtcreator/././libicudata.so.51 (0x00007fc5d2c97000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fc5d2a59000)not sure what it means though, or if that was what i was supposed to do :S
-
ldd search for all libraries that are linked to your library.
The thing is that you have found the qsqlite plugin from Qt Creator which is not the one you are using right now. You have to search in the folder where you have the Qt libraries which IIRC is at the same level as the Tools folder
-
After a long time, ive found the "error".
I was really surprised, because theres no actual problem in my code, and the documentation never spoke about these. I was also surprised that more people didnt have my problem, maybe its a combination of my qt version and drivers, no idea, but this is what was happening.The query must be created in the line after opening the database. for example
@
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE");
QSqlQuery query();
db.setDatabaseName("ejemplo.db3");
unsigned int useless int;
for(int i=0;i<255;i**)
useless int ++;
db.open();
Query.prepare("CREATE TABLE t1 (t1key INTEGER PRIMARY KEY,data TEXT,num double,timeEnter DATE)");
@
WONT WORK, but if you do this instead@
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("ejemplo.db3");
unsigned int useless int;
for(int i=0;i<255;i**)
useless int ++;
db.open();
QSqlQuery query();
Query.prepare("CREATE TABLE t1 (t1key INTEGER PRIMARY KEY,data TEXT,num double,timeEnter DATE)");
@
works like a charm -
Because you were doing it wrong (and not following the documentation examples)
addDatabase only tells what kind of database you will be using, you don't have any valid connection right now. So creating the query at this point is the same as trying to write to a file without path that you have not opened yet.