VS在win32平台与mysql链接_mysql5.5.28-win32 + qt--4.8.2-vs2008 数据库驱动编译与连接...
我們來編譯配置qt的mysql數(shù)據(jù)庫驅(qū)動,首先看看官方文檔的說明,如下
How to Build the QMYSQL Plugin on Windows
You need to get the MySQL installation files.
RunSETUP.EXEand choose "Custom Install".
Install the "Libs & Include Files" Module. Build
the plugin as follows (here it is assumed that MySQL is installed
inC:\MySQL):
cd %QTDIR%\src\plugins\sqldrivers\mysql
qmake "INCLUDEPATH+=C:\MySQL\include" "LIBS+=C:\MYSQL\MySQL Server \lib\opt\libmysql.lib" mysql.pro
nmake
If you are not using a Microsoft compiler,
replacenmakewithmakein
the line above.
Note:This database plugin is not
supported for Windows CE.
Note:Including"-o
Makefile"as
an argument toqmaketo
tell it where to build the makefile can cause the plugin to be
built in release mode only. If you are expecting a debug version to
be built as well, don't use
the"-o Makefile"option.
How to build the MySQL driver for MinGW users
The following steps have been used successfully for WinXP SP3.
In this example, Qt 4.6.2 is shown.
Download the following components:
MinGW-5.1.6.exe
mingw-utils-0.3.tar.gz
Qt sources, e.g.qt-everywhere-opensource-src-4.6.2.zip
mysql-5.1.35-win32.msi
InstallMinGW-5.1.6.exein,
e.g.C:\MinGW.
Extractmingw-utils-0.3.tar.gzinto,
e.g.C:\MinGW.
Add the path forMinGW-5.1.6.exeto
yourPATHvariable,
e.g.C:\MinGW\bin;
Extract the Qt sources, (qt-everywhere-opensource-src-4.6.2.zip),
into, e.g.C:\Qt.
Add the path for the eventual Qt binary to yourPATHvariable,
e.g.C:\Qt\4.6.2\bin;.
Install MySQL (mysql-5.1.35-win32.msi), customizing the
components. Select only the headers and libraries. Install in,
e.g.C:\MySQL\MySQL51.
Open the DOS prompt, go toC:\MySQL\MySQL51\lib\opt, and run the
following commands:
reimp -d libmysql.lib
dlltool -k -d libmysql.def -l
libmysql.a
Open the DOS prompt, go toC:\Qt\4.6.2and run the following
commands:
configure.exe -debug-and-release
-platform win32-g++ -qt-sql-mysql -l mysql -I
C:\MySQL\MySQL51\include -L C:\MySQL\MySQL51\lib\opt
mingw32-make sub-src
This step takes a long time.
Open the DOS prompt, go toC:\Qt\4.6.2\src\plugins\sqldrivers\mysqland
run the following command:
qmake
"INCLUDEPATH+=C:\MySQL\MySQL51\include" "LIBS+=-L. mysql"
mysql.pro
Now the following libraries are ready inC:\Qt\4.6.2\plugins\sqldrivers.
libqsqlmysql4.a
libqsqlmysqld4.a
qsqlmysql4.dll
qsqlmysqld4.dll
To use the SDK and QtCreator directly, copy these libraries to
yourC:\Qt\...\qt\plugins\sqldrivers\, and
copyC:\MySQL\MySQL51\lib\opt\libmysql.dllto
yourC:\Qt\...\qt\bin\.
我在安裝配置過程中首先參考的第二條,即"for MingW Users",但不管怎樣添加環(huán)境變量總是報
" 'mingw32-make'不是內(nèi)部或外部命令 "錯誤,將mingw32-make.exe直接拷貝到
C:\Qt\4.8.2\bin目錄下報如下錯誤
C:\Qt\2010.02.1\bin;
C:\Qt\2010.02.1\qt\bin;
C:\Qt\2010.02.1\mingw\bin;
C:\mysql\lib\opt;
C:\mysql\include;
C:\mysql\bin
分別對應(yīng)我的環(huán)境變量
C:\Qt\4.8.2\bin;
C:\Qt\4.8.2\mingw\bin(原目錄不存在,新建了mingw文件夾,再將
C:\MinGW\bin文件拷貝到新建的mingw文件中);
C:\MySQL\lib(同樣opt目錄不存在,
因為libmysql.lib在lib文件加下,所以沒有影響);
C:\MySQL\include; C:\MySQL\bin;
C:\MinGW\bin
添加完環(huán)境變量后運(yùn)行 mingw32-make出現(xiàn)上述錯誤,在dos
命令行下set path查看
環(huán)境變量如下
在qt command prompt中set path查看環(huán)境變量如下
可以看到,在qt command下沒有C:\MinGW\bin路徑,原因不明,求解釋。
在網(wǎng)上參考了很多資料,mingw32-make編譯配置qt mysql驅(qū)動仍不成功,最后直接nmake
編譯成功了。
我的nmake編譯過程如下,
1
由于MySQL的默認(rèn)安裝路徑中有空格,qmake工具無法編譯,所以先把MySQL安裝目錄下的include和lib目錄拷貝到?jīng)]有空格的路徑下,比如C:\MySQL(這步很重要)
2.打開Qt Command Prompt(注意是qt
command,而不是dos command,因為dos 可能無法識別qmake,nmake等
命令),輸入命令:cd
%QTDIR%\src\plugins\sqldrivers\mysql按回車鍵,然后輸入命令:
qmake “INCLUDEPATH+=C:\mysql\include”
“LIBS+=C:\mysql\lib\opt\libmysql.lib” mysql.pro,按回車鍵,報如下錯誤
WARNING: (internal):1: d backslashes are
deprecated. 這種錯誤的解決方法如下:
(1)進(jìn)入目錄E:\Qt\2010.05\qt\src\plugins\sqldrivers\mysql,用記事本打開mysql.pro,在第二行添加如下的信息:INCLUDEPATH+="C:\MySQL\include"
LIBS+="C:\MySQL\lib\opt\libmysql.lib"
(2)在dos下輸入:qmake -o
Makefile mysql.pro此時可能也會提示:
WARNING: e:\Qt\2010.05\qt\src\plugins\sqldrivers\mysql\mysql.pro:2:
d backslashes are
deprecated.
WARNING: e:\Qt\2010.05\qt\src\plugins\sqldrivers\mysql\mysql.pro:2:
d backslashes are
deprecated.
WARNING: e:\Qt\2010.05\qt\src\plugins\sqldrivers\mysql\mysql.pro:2:
d backslashes are
deprecated.
但是對最后的結(jié)果沒有影響。
3.nmake
4.nmake release
C:\Qt\4.8.2\src\plugins\sqldrivers\mysql\debug下的libqsqlmysqld4.a和qsqlmysqld4.dll文件拷貝到Qt安裝目錄\plugins\sqldrivers目錄下,這樣只生成了Qt在Debug模式下的MySQL驅(qū)動。在輸入命令mingw-32
make
release編譯Release模式下的MySQL驅(qū)動,編譯成功后將Qt安裝目錄\src\plugins\sqldrivers\mysql\release下的libqsqlmysqld4.a和qsqlmysqld4.dll文件拷貝到Qt安裝目錄\plugins\sqldrivers目錄下,這樣就完成了Qt在Release模式下的MySQL驅(qū)動。
注意,我最終編譯生成的只有qsqlmysql4.dll,
qsqlmysql4d.lib, qsqlmysql4.dll, qsqlmysql4.lib等文件,
并沒有l(wèi)ibmysqld4.a與libmysql4.a等文件,但將上述存在的4個文件拷到C:\Qt\4.8.2\plugins\sqldrivers目錄下測試仍然通過,可用。
執(zhí)行以上步驟后測試出現(xiàn)
QSqlDatabase: QMYSQL driver not loaded
將C:/MySQL/bin目錄下的libmySQL.dll文件復(fù)制到Qt安裝目錄下的C:\Qt\4.8.2\bin目錄中
之后再運(yùn)行測試程序即可正確建立連接并操作數(shù)據(jù)庫。
參考:
測試如下
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
// 連接mysql測試
bool createConnection()
{
QSqlDatabase db =
QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("root");
db.setPassword("test");
if(!db.open())
{
QMessageBox::critical( 0,
QObject::tr("Database error"), db.lastError().text());
return false;
}
return true;
}
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
if(!createConnection())
return 1;
QSqlQuery query;
query.exec("insert into test
values(1,'test')");
return app.exec();
}
使用Mysql workbench工具查看對應(yīng)數(shù)據(jù)庫即可。
若報"qsqldatabase"文件找不到等錯誤,則
到qt的安裝目錄下將對應(yīng)的頭文件添加到include中,或者將依賴的庫文件或頭文件加到系統(tǒng)環(huán)境變量中
與項目的附加依賴庫中,注意,include目錄與lib目錄應(yīng)分別添加。
如我的相關(guān)頭文件為和,打開cmd,輸入set
path可查看
已有環(huán)境變量,Qt相關(guān)的為
C:\Qt\4.8.2\lib;
C:\Qt\4.8.2;
如果還不行,最笨的方法,輸入完整路徑,如
include
若出現(xiàn)“無法解析的外部符號”錯誤,
則在VS
2008 的 Qt 菜單下點擊"qt project settings" 選項, 點擊"qt
modules"標(biāo)簽,
勾選需添加的對應(yīng)模塊,即sql library即可。
參考:
總結(jié)
以上是生活随笔為你收集整理的VS在win32平台与mysql链接_mysql5.5.28-win32 + qt--4.8.2-vs2008 数据库驱动编译与连接...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: ubuntu 编译mysql_Ubunt
 - 下一篇: mysql characteristic