Connect to the MySQL database in QT 4

Source: Internet
Author: User

I installed qt4for windows just the day before. I found that MySQL and other database plug-ins were not installed at the time of installation. Like QT X11, I needed to install these plug-ins myself.
In the installation directory c:/qt4.1.2/of QT, there is a src directory, enter plugins/sqldrivers/MySQL, and you can see two files, Main. CPP and MySQL. pro, it is clear that qmake is used to complete compilation and generation goals.
Editing
Before translating, ensure that the MySQL header file and library file on which the dependency is correct. First, copy all the include files in the MySQL installation directory to the directory that can be found during qmake, and then
Libmysql. A is copied under the LIB/OPT directory. In fact, only libmysql. lib and libmysql. dll are available under lib/OPT.
Method to generate libmysql. A from libmysql. dll:

1. Install MySQL (if you do not want to install it, you can find a libmysql. dll file)

2. Download The pexports Tool

3. Conversion operation: pexports libmysql. dll> libmysql. Def

4. Use the dlltool of mingw to convert it to the libmysql. A file. Dlltool -- input-Def libmysql. def -- dllname libmysql. dll -- output-lib libmysql. A-K

5. Check whether the attempt is successful:
Copy the generated libmysql. A to the Lib search path of qmake and run the following command in the plugins/sqldrivers/MySQL directory:
Qmake-Project
Qmake
Make

If the following error message is displayed: mysql_connect @ XX (XX is a number. Perform step 1.

6. Modify the libmysql. Def file and add @ XX to mysql_connect, that is, mysql_connect @ xx.

7. Redo Step 1

8. Then, try Step 1. If the error still persists. Step 2. Until there is no error.

Finally, libqsqlmysql. A and qsqlmysql. dll are generated under C:/qt4.1.2/plugins/sqldrivers.

I found a test file and passed it.
# Include <qtsql>
# Include <qtgui>


Bool createconnection (){
Qsqldatabase DB = qsqldatabase: adddatabase ("qmysql ");
DB. sethostname ("localhost ");
DB. setdatabasename ("test ");
DB. setusername ("test ");
DB. setpassword ("");

If (! DB. open ()){
Qmessagebox: Critical (0, qobject: TR ("database error "),
DB. lasterror (). Text ());

Return false;
}

Qsqlquery query;
// Query.exe C ("insert into book values (3, 'title', 'author ')");
Query.exe C ("select title, author from book ");

While (query. Next ()){


Qstring Title = query. Value (0). tostring ();
Qstring author = query. Value (1). tostring ();

Qmessagebox: Critical (0, title, author );

}


Return true;
}



Int main (INT argc, char ** argv ){

Qapplication app (argc, argv );
If (! Createconnection ()){

Return 1;
}

Return app.exe C ();

}

Original article: http://www.blogjava.net/felicity/archive/2006/08/28/66280.html

The best Google Android technology community in China (eoeandroid). Welcome to visit!

Galaxy series original tutorial released

Java Web development quick learning guide published, please order

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.