ubuntu10.04 Qt5.2.1链接mysql出现QMYSQL driver not loaded的问题!!!!

背景介绍:由于学校开设了数据库 课程,由此想用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

  1. Go to the plugins directory described above.
  2. Go inside sqldrivers directory.
  3. 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

  1. Download the latest source code of Qt from qt-project (It should be named like "qt-everywhere-opensource-src-version".tar.gz)
  2. unpack it and go to qtbase/src/plugins/sqldrivers/mysql/ directory
  3. >> qmake
  4. >> make
  5. 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


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值