QSQLITE driver not loaded - where to put qt databa

2019-01-17 16:50发布

问题:

I am using VS2008 & QT plugin to make my application. After making package when I am running the application I am getting error :

QSqlDatabase: QSQLITE driver not loaded
QSqlDatabase: available drivers: 
Database error: QSqlError(-1, "Driver not loaded", "Driver not loaded") 
QSqlError(-1, "Driver not loaded", "Driver not loaded") 

I have added the qsqlite.dll to my package & also changed the libpath. But still I am getting this error. How to solve this.

My Code::

  QStringList str;
str.append(".");
a.setLibraryPaths(str);
a.addLibraryPath("./sqldrivers/");

//a.addLibraryPath(".");

qDebug()<<"my library path : "<<a.libraryPaths();

QLibrary sqlib("qsqlite4.dll");
sqlib.load();
qDebug()<<"my library loaded"<<sqlib.isLoaded();

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
qDebug()<<"Database error:"<<db.lastError();

db.setDatabaseName("vimukti1234");
qDebug()<< db.lastError();

 db.open();
QSqlQuery query;

回答1:

The drivers need to be placed under "sqldrivers", not in the same directory as the executable (they are loaded on runtime, and Qt looks for them in "sqldrivers"). A typical structure of one of our installed applications is like this:

.:
total 26616
-rwxr-xr-x 1 root root 2245632 Sep 29 03:53 AlvaEditor.exe
-rwxr-xr-x 1 root root 2335232 Sep 29 03:53 QtCore4.dll
-rwxr-xr-x 1 root root 8421376 Sep 29 03:53 QtGui4.dll
-rwxr-xr-x 1 root root  199168 Sep 29 03:53 QtSql4.dll
-rwxr-xr-x 1 root root  306688 Sep 29 03:53 libctemplate.dll
-rwxr-xr-x 1 root root   26624 Sep 29 03:53 qgif4.dll
-rwxr-xr-x 1 root root   28672 Sep 29 03:53 qico4.dll
-rwxr-xr-x 1 root root  200704 Sep 29 03:53 qjpeg4.dll
-rwxr-xr-x 1 root root  222720 Sep 29 03:53 qmng4.dll
-rwxr-xr-x 1 root root  439808 Sep 29 03:53 qsqlite4.dll
-rwxr-xr-x 1 root root   21504 Sep 29 03:53 qsvg4.dll
-rwxr-xr-x 1 root root  287232 Sep 29 03:53 qtiff4.dll
drwxr-xr-x 2 root root    4096 Sep 29 03:53 sqldrivers

./sqldrivers:
total 432
-rwxr-xr-x 1 root root 439808 Sep 29 03:53 qsqlite4.dll


回答2:

Well, the function: addDatabase("QSQLITE"); takes two parameters, the first is the driver and the second is the name of your connection, (passed as a QString)

Now, try the following:

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

It worked for me, so I guess it'll work for you. (Assuming SQLITE is among your installed drivers)

You can check for SQLITE by the following:

qDebug()  <<  QSqlDatabase::drivers();

Good luck!

Zaher J.G.



回答3:

Just Add dll file which have folder life platform and drivesr

so simply build app using windeployqt tool



回答4:

Try this first:

qDebug() << QSqlDatabase::drivers();

to check available Drivers.