Today I would like to try Qt How to connect the database. Who knows how to write the hint driver not loaded I was depressed. I myself is VS2010 + Qt4.8.4 + MySQL5.1 environment on-line check is C:\Qt\4.8.4\plugins\sqldrivers without MySQL dll and lib file. So you have to compile it yourself.
1. Enter ($QtDir $) through the QT 4.8.4 command prompt line \src\plugins\sqldrivers\mysql
2. Because the absolute path of MySQL has a space, makefile cannot be found accurately, so in any disk Descriptor directory new folder MySQL (such as C:\MySQL), and Will ($MySQLDir $) \inlcude with ($MySQLDir $) \ The Lib folder is copied to the folder 2. Enter command qmake-o makefile "Includepath+=c:\mysql\include" "Libs+=c:\mysql\lib\opt\libmysql.lib" Mysql.pronmake3. View ($QtDir $) \plugins\sqldrivers folder to generate Sqlmysql4.dll, Qsqlmysql4.lib, Qsqlmysqld4.dll, Qsqlmysqld4.lib
[CPP]View PlainCopy
- Qsqldatabase db = Qsqldatabase::adddatabase ("Qmysql");
- Db.sethostname ("localhost");
- Db.setdatabasename ("MyDB");
- Db.setusername ("root");
- Db.setpassword ("");
- if (!db.open ())
- Qmessagebox::information (this,tr (""), Db.lasterror (). text ());
See success?
8820652
Qt Connection MySQL database (many related articles)