Windows平台下編譯Qt的MySQL資料庫驅動

來源:互聯網
上載者:User

       先吐個槽,弄了將近一天的時間才弄好,其實大概在一年之前弄個,不過當時沒記錄下來,導致現在又得再做一遍,可見部落格是多麼的重要啊!花了這麼多時間主要是因為想從源碼編譯,大家都知道,編譯一個Qt源碼真心不容易啊,好久不能關電腦!!!好了,吐完了,說正事!

        最近最項目,選擇了Qt和MySQL組合。之前一直在linux下做事,可惜這次要求環境是Windows,所以得重新搭建環境。首先安裝Qt的開發環境,我直接選擇的QtSDK,一個包安裝到底就行;MySQL也是官網下載最新安裝包安裝的。兩個部分單獨測試過,都已經正常工作了!接下來就需要Qt操作MySQL了,這需要MySQL資料庫驅動,QtSDK安裝後只是內建了SQLite資料庫的驅動,需要自己編譯。我選擇的編譯工具是大名鼎鼎的MinGW,然後需要Qt源碼包,我選擇的版本是4.7.4。

       接下來就是編譯了,感覺無從下手的時候最好就看看Qt的協助文檔,至於怎麼使用Qt的協助文檔,可以看看上一篇博文,這裡貼一下在協助文檔中搜尋到的結果:

        這篇協助文檔裡面非常詳細的介紹了如何編譯各種資料庫的驅動,我們這裡選擇在Windows下編譯MySQL資料庫驅動。

         文檔十分詳細,基本都是步驟,我就翻譯了,直接,大家一看就知道:

 

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
  • Install MinGW-5.1.6.exe in, e.g. C:\MinGW.
  • Extract mingw-utils-0.3.tar.gz into, e.g. C:\MinGW.
  • Add the path for MinGW-5.1.6.exe to your PATH variable, 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 your PATH variable, 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 to C:\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 to C:\Qt\4.6.2 and 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 to C:\Qt\4.6.2\src\plugins\sqldrivers\mysql and run the following command:

    • qmake "INCLUDEPATH+=C:\MySQL\MySQL51\include" "LIBS+=-L. mysql" mysql.pro
  • Now the following libraries are ready in C:\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 your C:\Qt\...\qt\plugins\sqldrivers\, and copyC:\MySQL\MySQL51\lib\opt\libmysql.dll to your C:\Qt\...\qt\bin\.

         這裡我貼一下自己編譯完成後的檔案,熟話說:有圖有真相,呵呵!

          將產生的檔案按照協助文檔說的,拷貝到相應的目錄下;接下來我們在QtCreator裡面寫一個程式,測試一下我們的驅動是否可用:

          程式的功能是輸出當前系統中Qt自主資料庫驅動;源碼如下:

 
 
   
#include <QApplication>#include <QSqlDatabase>#include <QStringList>#include <QDebug>int main(int argc, char* argv[]) {    QApplication app(argc, argv);    qDebug() << "Available drivers:";    QStringList drivers = QSqlDatabase::drivers();    foreach(QString driver, drivers)        qDebug() << "\t" << driver;    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");    qDebug() << "ODBC driver valid?" << db.isValid();}
 程式輸出結果如下:

         由上面輸出我們可以看出:Qt內建的SQLite和我們自己編譯的MySQL資料庫的驅動都可以正常工作了,我們沒有編譯的ODBC資料庫驅動就沒有支援!

 

注意:

        為什麼這麼簡單的東西我卻花了將近一天的時間呢?這裡說一下自己的思路和做法,避免大家走彎路,更讓大家熟悉怎麼編譯Qt模組。

        最初我是直接編譯Qt源碼,沒有利用已安裝的Qt工具來模組化編譯資料庫驅動,這出現了兩個問題:

         1、直接編譯,花費很長時間才能編譯完;若利用configure組態工具驅動一些模組後會編譯報錯,哎;

         2、這個時候指定MySQL標頭檔和庫檔案路勁比較糾結,可能是我對Windows不熟悉了,反正我覺得糾結。

         後來我才準備模組化編譯的,這個確實比較簡單,一會就弄完了,特別要注意上面要利用MinGW工具對MySQL庫檔案進行轉換。

 

         好了,就到這吧!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.