MySQL QMYSQL 驱动可用但未加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14667768/
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
QMYSQL driver available but not loaded
提问by Hannoun Yassir
How do I load qmysql
driver in Qt? I have the following code that produces these results:
如何qmysql
在 Qt 中加载驱动程序?我有以下代码产生这些结果:
("QSQLITE", "QMYSQL", "QMYSQL3")
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3
Any suggestions on how to load it?
关于如何加载它的任何建议?
采纳答案by Hannoun Yassir
ok it worked just by copying the sqldrivers
folder to my debug folder and it worked!
好的,只需将sqldrivers
文件夹复制到我的调试文件夹即可工作,并且工作正常!
回答by Desmond
We should check our driver first
我们应该先检查我们的司机
$ cd /opt/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers
then we can find some files
然后我们可以找到一些文件
Use the command below to check library
使用下面的命令检查库
$ ldd libqsqlmysql.so
if you find the problem libmysqlclient_r.so.16 => not found
it may be the library-dependency problem.
如果您发现问题,libmysqlclient_r.so.16 => not found
则可能是库依赖问题。
After I did a little research on the Internet, there is a way would be easy.
在我在互联网上做了一些研究之后,有一种方法会很容易。
$ cd /usr/lib/x86_64-linux-gnu
if you find libmysqlclient_r.so.18
,
如果你发现libmysqlclient_r.so.18
,
$ cp libmysqlclient_r.so.18 libmysqlclient_r.so.16
回答by handle
On Windows (see as directory structure):
在 Windows 上(参见目录结构):
the_qt_app.exe
libmysql.dll
sqldrivers/qsqlmysql4.dll
回答by qdot
You could try diagnosing the issue with strace
- it seems like the QMYSQL driver might need some run-time library dependencies to work.
您可以尝试诊断问题strace
- 似乎 QMYSQL 驱动程序可能需要一些运行时库依赖项才能工作。
回答by Niklas
You can use QPluginLoader
to get some better error message.
您可以使用QPluginLoader
来获得一些更好的错误消息。
When I had the same problem with the MySQL driver the message was something like "The version was compiled with other options than this Qt version".
当我在 MySQL 驱动程序上遇到同样的问题时,消息类似于“该版本是使用此 Qt 版本以外的其他选项编译的”。
It seemed like the Qt sources, that shipped with the Qt SDK at that time, were not compatible with its binaries.
当时 Qt SDK 附带的 Qt 源似乎与其二进制文件不兼容。
After downloading the Qt sources and compiling my own version of Qt and the MySQL driver, the problem was gone.
下载 Qt 源代码并编译我自己的 Qt 版本和 MySQL 驱动程序后,问题就消失了。
EDIT: Some sample code.
编辑:一些示例代码。
QPluginLoader loader;
loader.setFileName("/Users/niklaswulf/QtSDK/Qt/4.8.4/plugins/sqldrivers/libqsqlite_debug.dylib");
qDebug() << loader.load();
qDebug() << loader.errorString();
loader.setFileName("/Users/niklaswulf/QtSDK/Qt/5.0.1/5.0.1/clang_64/plugins/sqldrivers/libqsqlite_debug.dylib");
qDebug() << loader.load();
qDebug() << loader.errorString();
When compiling against 5.0.1 this is the output:
针对 5.0.1 编译时,这是输出:
false
"The file '/Users/niklaswulf/QtSDK/Qt/4.8.4/plugins/sqldrivers/libqsqlite_debug.dylib' is not a valid Qt plugin."
true
"Unknown error"
I also found the old message:
我还发现了旧消息:
The plugin '/path/to/some/libqsqlmysql.dylib' uses incompatible Qt library. Expected build key "macosx macx-cocoa g++-4 full-config", got "macosx macx-cocoa g++-4 no-pkg-config"
回答by Rahul chand verma
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3
The same problem I faced in fedora20 (64-bit) with Qt-5.2.0, and then follow steps:
我在使用 Qt-5.2.0 的 fedora20(64 位)中遇到的同样问题,然后按照以下步骤操作:
$ cd /opt/Qt5.2.0/5.2.0/gcc_64/plugins/sqldrivers
$ ls
libqsqlite.so libqsqlmysql.so
Use the command below to check library dependency:
使用以下命令检查库依赖项:
$ ldd libqsqlmysql.so
I find the problem:
我发现问题:
libmysqlclient_r.so.16 => not found
It may be the library-dependency problem. so solve this problem:
这可能是库依赖问题。所以解决这个问题:
Linking of the library file:
链接库文件:
$ ln -s libmysqlclient_r.so.16.0.0 libmysqlclient_r.so
and again:
然后再次:
$ ln -s libmysqlclient_r.so.16.0.0 libmysqlclient_r.so.16
Now its work for me. All the best
现在它为我工作。祝一切顺利
回答by Nicolas C. R.
Got the same problem and some Google research and intuition finally solved it. Using Qt5.9.1 and Ubuntu 17.10
遇到了同样的问题,谷歌的一些研究和直觉终于解决了。使用 Qt5.9.1 和 Ubuntu 17.10
First, check if the error of libmysqlclient.so.18 => not found
is present
首先,检查的误差libmysqlclient.so.18 => not found
是存在
:~/Qt5.9.1/5.9.1/gcc_64/plugins/sqldrivers$ ldd libqsqlmysql.so
Second, search where is libmysqlclient
二、搜索libmysqlclient在哪里
:/$ locate libmysqlclient
Third, go to the folder where libmysqlclient is present and there make the link
第三,转到存在 libmysqlclient 的文件夹并在那里制作链接
:/usr/lib/x86_64-linux-gnu$ sudo ln -s libmysqlclient.so.20 libmysqlclient.so.18
and check the link made before with
并检查之前制作的链接
ls -alh | grep libmysql
At that moment, none of those solved for me, and i decided to look further in synaptic packages, and realize that libqt5sql5-mysql
version 5.9.1 was not installed, so installing it solved the problem but i still have a message when doing ldd
在那一刻,这些都没有为我解决,我决定进一步查看突触包,并意识到libqt5sql5-mysql
未安装 5.9.1 版,因此安装它解决了问题,但在执行 ldd 时我仍然收到一条消息
./libqsqlmysql.so: /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18: version `libmysqlclient_18' not found (required by ./libqsqlmysql.so)
./libqsqlmysql.so: /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18: 找不到版本`libmysqlclient_18'(./libqsqlmysql.so 需要)
after that, found some links that guide me to a real solution, here are them, if you wanna know whats happening
在那之后,找到了一些指导我找到真正解决方案的链接,如果你想知道发生了什么,这里是它们
i downloaded the library stated in the third link and worked like a charm. hope it helps!
我下载了第三个链接中所述的库,并且工作得很有魅力。希望能帮助到你!
回答by Peyman
Here a couple of very nice links on this issue:
这里有几个关于这个问题的非常好的链接:
making the plugins manually on Unix-based systems (from Qt documentation): http://qt-project.org/doc/qt-5/sql-driver.html
a very well-written link specifically on this issue (Do not forget to install the Qt from source, this can be done by either checking the corresponding (hidden) box in the first step while installing from the .run executable or by downloading the 'qt-everywhere-opensource-src' version): http://adamcavendish.is-programmer.com/posts/40431.html
在基于 Unix 的系统上手动制作插件(来自 Qt 文档):http: //qt-project.org/doc/qt-5/sql-driver.html
一个专门针对这个问题的写得很好的链接(不要忘记从源代码安装 Qt,这可以通过在从 .run 可执行文件安装时在第一步中选中相应的(隐藏)框或通过下载 ' qt-everywhere-opensource-src' 版本):http: //adamcavendish.is-programmer.com/posts/40431.html
Enjoy, Peyman
享受,佩曼
回答by Ingwar Smith
My answer:
我的答案:
QSqlDatabase db(QSqlDatabase::addDatabase("QMYSQL"));
db.setDatabaseName("dbname");
db.setHostName("localhost");
db.setUserName("usernm");
db.setPassword("password");
if (db.open())
{
qDebug() << "SUCCESS!";
db.close();
}