Ubuntu下Qt连接Mysql失败问题
代码:
QSqlDatabase dbConn = QSqlDatabase::addDatabase("QMYSQL");
dbConn.setHostName(m_hostName);
dbConn.setDatabaseName(m_dbName);
dbConn.setUserName(m_userName);
dbConn.setPassword(m_password);
if (!dbConn.open())
{
qDebug() << "open db failed:" << dbConn.lastError().text();
return;
}
qDebug() << "open mysql...";
dbConn.close();
使用Qt5.12.9开发,出现问题如下:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7
open db failed: "Driver not loaded Driver not loaded"
分析:
available drivers中不包含QMYSQL,说明Qtxxx/xxx/gcc_64/plugins/sqldrivers下没有libqsqlmysql.so
这个库,需要自己编译或者下载带这个库的Qt版本
现在安装Qt5.12.3版本,这个版本自带libqsqlmysql.so
这个库
切换到Qt5.12.3版本后,运行代码,报错如下
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
open db failed: "Driver not loaded Driver not loaded"
分析:
available drivers中包含QMYSQL,但依然出现 QMYSQL driver not loaded
,说明 libqsqlmysql.so
依赖可能有问题
# 到Qt安装目录
cd Qt5.12.3/5.12.3/gcc_64/plugins/sqldrivers
# 查看库依赖
ldd libqsqlmysql.so
# 结果:
linux-vdso.so.1 (0x00007ffd0f8a6000)
libQt5Sql.so.5 => /home/dong/Qt5.12.3/5.12.3/gcc_64/plugins/sqldrivers/./../../lib/libQt5Sql.so.5 (0x00007fc40695f000)
libQt5Core.so.5 => /home/dong/Qt5.12.3/5.12.3/gcc_64/plugins/sqldrivers/./../../lib/libQt5Core.so.5 (0x00007fc4061cc000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc406187000)
libmysqlclient.so.18 => not found
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fc405fa6000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fc405e55000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fc405e3a000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc405c48000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fc405c2c000)
libicui18n.so.56 => /home/dong/Qt5.12.3/5.12.3/gcc_64/plugins/sqldrivers/./../../lib/libicui18n.so.56 (0x00007fc405793000)
libicuuc.so.56 => /home/dong/Qt5.12.3/5.12.3/gcc_64/plugins/sqldrivers/./../../lib/libicuuc.so.56 (0x00007fc4053db000)
libicudata.so.56 => /home/dong/Qt5.12.3/5.12.3/gcc_64/plugins/sqldrivers/./../../lib/libicudata.so.56 (0x00007fc4039f6000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc4039f0000)
libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007fc4039eb000)
libglib-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007fc4038c2000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc406dbc000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fc40384f000)
可以看到 libmysqlclient.so.18 => not found
接下来安装libmysqlclient.so.18
这个库即可
安装:
sudo dpkg -i libmysqlclient18_5.6.25-0ubuntu1_amd64.deb
网友评论