In Windows, we use QT to develop database applications, such as MySQL. First, we must use the source code to compile the MySQL driver library;
1)install qt-sdk-win-opensource-2010.02.1.exe
In this case, QT creator is installed on QT. Assume that the directory is:
E:/QT/2010.02.1/. You can choose to install mingw;
In this case, E:/QT/2010.02.1/Qt is the installation location of the QT library;
2) install mysql. Assume that the directory is:
Include path: D:/MySQL Server 5.0/include
Lib path: D:/MySQL Server 5.0/lib/OPT
Because there are spaces in the two paths, copy them to the C:/MySQL folder;
3) add E:/QT/2010.02.1/QT/bin and mingw/bin to the path;
4) cd e:/QT/2010.02.1/QT/src/plugins/sqldrivers/MySQL folder
Run:
E:/QT/2010.02.1/QT/src/plugins/sqldrivers/mysql> qmake-o
Makefile "includepa
Th + = C:/MySQL/include"
"Libs + = C:/MySQL/lib/opt/libmysql. lib" mysql. Pro
Nmake
Generate the MySQL driver in E:/QT/2010.02.1/QT/plugins/sqldrivers;
Write the database access code:
View
Plain
Copy
To clipboard
Print
?
- Qsqldatabase DB = qsqldatabase: adddatabase (
"Qmysql"
);
- DB. sethostname ("127.0.0.1"
);
- DB. setport (3306 );
- DB. setdatabasename ("test"
);
- DB. setusername ("root"
);
- DB. setpassword ("**********"
);
- If
(! DB. open ()){
- Qmessagebox: Warning (this
,
"Warning! "
,
"Failure"
);
- } Else
{
- Qmessagebox: Information (this
,
"OK! "
,
"Success! "
);
- }
Qsqldatabase <br/> DB = qsqldatabase: adddatabase ("qmysql"); <br/> dB. sethostname ("127.0.0.1"); <br/> dB. setport (3306); <br/> dB. setdatabasename ("test"); <br/> dB. setusername ("root"); <br/> dB. setpassword ("***********"); <br/> If (! DB. open () {<br/> qmessagebox: Warning (this, "warning! "," Failure "); <br/>}else {<br/> qmessagebox: Information (this," OK! "," Success! "); <Br/>}
Access successful;
The following is the connection string between ODBC and oledb:
ODBC connection
Suitable
Database Connection Mode
Access "driver = {Microsoft Access
Driver (*. mdb)}; DBQ = *. mdb; uid = admin; Pwd = pass ;"
DBASE "driver = {Microsoft DBASE
Driver (*. DBF)}; driverid = 277; DBQ = ------------;"
Oracle "driver = {Microsoft ODBC
For Oracle}; server = oraclesever. World; uid = admin; Pwd = pass ;"
MSSQL
Server "driver = {SQL
Server}; server = servername; database = dbname; uid = sa; Pwd = pass ;"
MS
Text "driver = {Microsoft
Text Driver (*. txt; *. CSV)}; DBQ = -----; extensions = ASC, CSV, tab, txt; persist
Securityinfo = false ;"
Visual FoxPro "driver = {Microsoft Visual FoxPro
Driver}; sourcetype = dBc; sourcedb = *. dBc; exclusive = no ;"
MySQL "driver = {MySQL}; database = yourdatabase; uid = username; Pwd = yourpassword; option = 16386 ;"
SQLite "driver = {sqlite3 ODBC
Driver}; database = D:/SQLite/*. DB"
PostgreSQL "driver = {PostgreSQL
ANSI}; server = 127.0.0.1; uid = admin; Pwd = pass; database = databasename"
Oledb
Connection
Suitable Connection Methods for database types
Access "provider = Microsoft. Jet. oledb.4.0; Data
Source = your_database_path; user id = admin; Password = pass ;"
Oracle "provider = oraoledb. Oracle; Data
Source = dbname; user id = admin; Password = pass ;"
SQL MS
Server "provider = sqloledb; Data
Source = machinename; initial catalog = dbname; userid = sa; Password = pass ;"
MS
Text "provider = microsof. Jet. oledb.4.0; Data
Source = your_path; extended properties 'text; FMt = delimited '"