PDA

View Full Version : QMysql Driver not loaded



Cerberus
29th October 2015, 15:10
When executing the following code I get an error that the Mysql driver is not loaded:



db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setDatabaseName("test");
db.setUserName("test");
db.setPassword("test");

if (!db.open()) {
QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
} else
qDebug() << "OPEN";



and in my .pro file I added it with



QT += core gui sql



I'm using Ubuntu 15.10 and I have installed
libqt5sql5-mysql
libqt4-sql-mysql

and when locating them with I get


locate libqsql
/usr/lib/i386-linux-gnu/qt4/plugins/sqldrivers/libqsqlmysql.so
/usr/lib/x86_64-linux-gnu/qt4/plugins/sqldrivers/libqsqlite.so
/usr/lib/x86_64-linux-gnu/qt5/plugins/sqldrivers/libqsqlite.so


also when running the ldd command all dependencies can be found:


ldd libqsqlite.so
linux-vdso.so.1 => (0x00007fff631e6000)
libsqlite3.so.0 => /usr/lib/x86_64-linux-gnu/libsqlite3.so.0 (0x00007f831f6d3000)
libQt5Sql.so.5 => /usr/lib/x86_64-linux-gnu/libQt5Sql.so.5 (0x00007f831f691000)
libQt5Core.so.5 => /usr/lib/x86_64-linux-gnu/libQt5Core.so.5 (0x00007f831f1d4000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f831ee52000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f831ea88000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f831e869000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f831e665000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f831e44b000)
libicui18n.so.55 => /usr/lib/x86_64-linux-gnu/libicui18n.so.55 (0x00007f831dfe7000)
libicuuc.so.55 => /usr/lib/x86_64-linux-gnu/libicuuc.so.55 (0x00007f831dc53000)
libpcre16.so.3 => /usr/lib/x86_64-linux-gnu/libpcre16.so.3 (0x00007f831d9f0000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f831d6e0000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f831d4d8000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f831d1d0000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f831cfb8000)
/lib64/ld-linux-x86-64.so.2 (0x0000557933e71000)
libicudata.so.55 => /usr/lib/x86_64-linux-gnu/libicudata.so.55 (0x00007f831b501000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f831b293000)


Do I need to do something else?

creatron
1st November 2015, 10:47
If you use QtCreator you need the mysql plugin. This is copied into the install directory
.... /plugins/sqldrivers

You need the following

libqsqlite.so
libqsqlmysql.so
libqsqlpsql.so

You can either create this with


cd $QTDIR/src/plugins/sqldrivers/mysql
qmake "INCLUDEPATH+=/usr/include/mysql" "LIBS+=-L/usr/lib/mysql -lmysqlclient_r" mysql.pro
make
[>CODE]
or just use QtCreator and open the above project files, and compile, it could moan and display a error message as one cannot run a plugin directly.
Now one just has to copy the created xx.so files into the plugin directory.

On Ubuntu , you may need to install the development environment with
[CODE]
sudo apt-get install libmysqlclient15-dev

ChrisW67
1st November 2015, 19:55
You seem to have three different Qt versions, 32 bit Qt4 and 64 bit Qt4 and Qt5. You need a consistent set of library of one "bitness". The Qt plugin for Mysql is already present for the basic 32bit Qt4 shown in the original post. These plugins are dependent on the Mysql runtime libraries (not part of Qt) that are likely not installed by default. You can check using ldd on the Mysql Qt plugin (not the Sqlite Qt plugin).

If needed, use something like this to install the Mysql client library

sudo apt-get install libmysqlclient15