标签:
在Windows环境中使用Qt进行关于MySQL数据库的操作时,会出现如下问题:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC3 QODBC
这是由于MySQL数据库的驱动程序未在Qt下编译造成的(确切的说是未使用mingw32进行编译),解决的方法如下:
1. 自定义安装MySQL,注意安装目录不要有空格,我的目录在
C:\MySQL\MySQL Server 5.6
虽然二级目录有空格,但是没有影响。
2. 打开Qt Command Prompt,切换到目录:
cd %QTDIR%\src\plugins\sqldrivers\mysql
3. 执行如下命令:
qmake "INCLUDEPATH+=C:\MySQL\MySQL Server 5.6\include" "LIBS+=C:\MySQL\MySQL Server 5.6\lib\libmysql.lib" mysql.pro
注意上述目录与安装的MySQL版本有关,本人使用的版本为:
mysql-installer-community-5.6.26.0.msi
4. 执行
mingw32-make
此处可能出现如下错误:
In file included from main.cpp:44: ../../../sql/drivers/mysql/qsql_mysql.h:52:19: error: mysql.h: No such file or directory In file included from main.cpp:44: ../../../sql/drivers/mysql/qsql_mysql.h:108: error: expected ‘)‘ before ‘*‘ token mingw32-make[1]: *** [tmp/obj/debug_shared/main.o] Error 1 mingw32-make[1]: Leaving directory `C:/Qt/2010.02.1/qt/src/plugins/sqldrivers/mysql‘ mingw32-make: *** [debug-all] Error 2
这是由于未包含MySQL库文件造成的,在mysql.pro文件中添加如下代码:
INCLUDEPATH += "C:/MySQL/MySQL Server 5.6/include" LIBS += "C:/MySQL/MySQL Server 5.6/lib/libmysql.lib"
重复步骤4.即可生成以下4个文件:
qsqlmysql4.dll
libqsqlmysql4.a
qsqlmysqld4.dll
libqsqlmysqld4.a
5. 将C:\MySQL\MySQL Server 5.6\lib\中的libmysql.lib拷贝到C:\Qt\2010.05\mingw\bin中。
//正文完
参考:
1. http://blog.csdn.net/wang_xuehen/article/details/7483133
2. http://blog.csdn.net/wang19870102/article/details/39318225
3. http://www.cppblog.com/biao/archive/2011/10/29/159296.html
标签:
原文地址:http://www.cnblogs.com/okstill/p/5026469.html