背景介绍:由于学校开设了数据库 课程,由此想用Qt来链接mysql来试试手~
可是在尝试C++ GUI Qt4这书的数据库例子时,就出现了QMYSQL driver not loaded 的问题
网上有很多办法,但是没有解决好问题的.......
情急之下翻墙去google看国外牛人是怎么解决的.........
果然发现了解决方法!以下是转载!
Qt 5.1.0 on Ubuntu Linux, MySQL database connection problem, driver not loaded problem
Problem description
1
2
3
|
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
false
|
Example Code to reproduce the error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <QCoreApplication>
#include <QtSql>
int
main(
int
argc,
char
*argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase(
"QMYSQL"
);
db.setHostName(
"localhost"
);
db.setDatabaseName(
"test"
);
db.setUserName(
"test"
);
db.setPassword(
"xxxxxxxxxx"
);
bool
ok = db.open();
qDebug() << ok;
db.close();
return
a.exec();
}
|
WTF:
I saw available drivers right there -- f**king "QMYSQL" there, but why am I not be able to load it?!
Ok, ignore me.
Problem Explanation:
No matter it's there or not it still may cause an error -- if you know c++ well, you gotta know that a library dependency problem is a huge headache anyway. (well, some may argue that it's a problem not only within c++. ok, ok... you're right, so what?)
- If you're using Qt on Ubuntu Linux, and you're installing with the ".run" binary installer of Qt officially. They might have already install the MySQL plugins for you inside the folder:
$QT_INSTALL_PLACE/$QT_VERSION_NUMBER/$COMPILE_TOOL_KIT_NAME/plugins
(my environment example is: /opt/Qt5.1/5.1.0/gcc_64/plugins/ folder).
- If you're using apt-get to install the qt5-default package, the plugins where mysql uses is located at:
/usr/lib/i386-linux-gnu/$QT_VERSION/plugins/ for 32bits ubuntu
or
/usr/lib/x86_64-linux-gnu/$QT_VERSION/plugins/ for 64bits ubuntu
How to check whether I have mysql driver, or have the right one
- Go to the plugins directory described above.
- Go inside sqldrivers directory.
- Check whether there is a library called libqsqlmysql.so inside.
If libqsqlmysql.so is inside, then you can get QMYSQL inside the QSqlDatabase: available drivers list described in the example code above.
However, the library is not granted to be working due to update of your mysql package or other misoperations.
Use the command below to check whether the library is under a library-dependency problem:
1
|
>> ldd libqsqlmysql.so
|
What generates in my environment is this:
You now may see the problem: libmysqlclient_r.so.16 => not found ---- there's a library he can't find. (Why cannot he find it? Because I updated the mysql package after I installed the Qt.)
Problem Solve
Recompile the plugin
- Download the latest source code of Qt from qt-project (It should be named like "qt-everywhere-opensource-src-version".tar.gz)
- unpack it and go to qtbase/src/plugins/sqldrivers/mysql/ directory
- >> qmake
- >> make
- It will generate the right lib file for you in the qtbase/plugins/sqldrivers/ directory
Check the recompiled the plugin
1
|
>> ldd libqsqlmysql.so
|
What generates in my environment is this:
With libmysqlclient.so.18 => /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18 (0x00007f98f988d000) this library, it finally gets to the correct dependent library.
Copy back the library Copy back the correct libqsqlmysql.so to the place it should be in:
$QT_INSTALL_PLACE/$QT_VERSION_NUMBER/$COMPILE_TOOL_KIT_NAME/plugins
or
/usr/lib/i386-linux-gnu/$QT_VERSION/plugins/
or
/usr/lib/x86_64-linux-gnu/$QT_VERSION/plugins/
Nota Bene:
- If you still cannot get to the right thing, you may have your environment set unproperly. Try this:
1
2
3
4
5
|
export
PATH=
"$QT_INSTALL_PLACE/$QT_VERSION_NUMBER/$COMPILE_TOOL_KIT_NAME/bin/"
:$PATH
export
LD_LIBRARY_PATH=
"$QT_INSTALL_PLACE/$QT_VERSION_NUMBER/$COMPILE_TOOL_KIT_NAME/lib/"
:$LD_LIBRARY_PATH
export
LD_LIBRARY_PATH=
"$QT_INSTALL_PLACE/$QT_VERSION_NUMBER/$COMPILE_TOOL_KIT_NAME/plugins/"
:$LD_LIBRARY_PATH
export
LIBRARY_PATH=
"$QT_INSTALL_PLACE/$QT_VERSION_NUMBER/$COMPILE_TOOL_KIT_NAME/lib/"
:$LIBRARY_PATH
export
LIBRARY_PATH=
"$QT_INSTALL_PLACE/$QT_VERSION_NUMBER/$COMPILE_TOOL_KIT_NAME/plugins/"
:$LIBRARY_PATH
|
My Environment Example:
1
2
3
4
5
|
export
PATH=
"/opt/Qt5.1/5.1.0/gcc_64/bin/"
:$PATH
export
LD_LIBRARY_PATH=
"/opt/Qt5.1/5.1.0/gcc_64/lib/"
:$LD_LIBRARY_PATH
export
LD_LIBRARY_PATH=
"/opt/Qt5.1/5.1.0/gcc_64/plugins/"
:$LD_LIBRARY_PATH
export
LIBRARY_PATH=
"/opt/Qt5.1/5.1.0/gcc_64/lib/"
:$LIBRARY_PATH
export
LIBRARY_PATH=
"/opt/Qt5.1/5.1.0/gcc_64/plugins/"
:$LIBRARY_PATH
|
This means you should have the plugins/ directory search availa