C++ 未加载 QSQLITE 驱动程序 - 将 qt 数据库驱动程序插件放在哪里

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5151279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:31:09  来源:igfitidea点击:

QSQLITE driver not loaded - where to put qt database driver plugins

c++visual-studiosqliteqt

提问by amrit_neo

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

我正在使用 VS2008 和 QT 插件来制作我的应用程序。在我运行应用程序时制作包后,我收到错误:

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.

我已将 qsqlite.dll 添加到我的包中并更改了 libpath。但我仍然收到此错误。如何解决这个问题。

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;

回答by qdot

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:

驱动程序需要放置在“sqldrivers”下,而不是与可执行文件位于同一目录中(它们在运行时加载,Qt 在“sqldrivers”中查找它们)。我们安装的应用程序之一的典型结构是这样的:

.:
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

回答by Zaher

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)

好了,函数: addDatabase("QSQLITE"); 需要两个参数,第一个是驱动程序,第二个是您的连接名称,(作为 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)

它对我有用,所以我想它对你有用。(假设 SQLITE 是您安装的驱动程序之一)

You can check for SQLITE by the following:

您可以通过以下方式检查 SQLITE:

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

Good luck!

祝你好运!

Zaher J.G.

扎赫尔

回答by Dnyaneshwar

Just Add dll file which have folder life platform and drivesr

只需添加具有文件夹生命平台和驱动程序的dll文件

so simply build app using windeployqttool

所以只需使用windeployqt工具构建应用程序

回答by Rukmananda Reddy

Linux platform: Build your Qt Source with the BR2_PACKAGE_QT5BASE_SQLITE_QT=yoption enabled in .configfile, and copy sqldrivers generated in output path lib/qt/plugins/sqldrivers/libqsqlite.soto /usr/lib/qt/plugins/sqldrivers/ on Target board and run your application.
Also you can Check where and all your binary/application looks for libraries and plugins using "QApplication::libraryPaths()" API

Linux 平台:使用文件中BR2_PACKAGE_QT5BASE_SQLITE_QT=y启用的选项构建您的 Qt 源.config,并将输出路径中生成的 sqldrivers 复制lib/qt/plugins/sqldrivers/libqsqlite.so到目标板上的 /usr/lib/qt/plugins/sqldrivers/ 并运行您的应用程序。
您还可以使用“QApplication::libraryPaths()”API 检查您的二进制文件/应用程序在何处以及所有查找库和插件的位置

回答by Harshi Srivastava

Try this first:

先试试这个:

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

to check available Drivers.

检查可用的驱动程序。