Connect to the MySQL database using QT in Windows

Source: Internet
Author: User
Tags dbase odbc connection

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
?
  1. Qsqldatabase DB = qsqldatabase: adddatabase (
    "Qmysql"
    );
  2. DB. sethostname ("127.0.0.1"
    );
  3. DB. setport (3306 );
  4. DB. setdatabasename ("test"
    );
  5. DB. setusername ("root"
    );
  6. DB. setpassword ("**********"
    );
  7. If
    (! DB. open ()){
  8. Qmessagebox: Warning (this
    ,
    "Warning! "
    ,
    "Failure"
    );
  9. } Else
    {
  10. Qmessagebox: Information (this
    ,
    "OK! "
    ,
    "Success! "
    );
  11. }

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 '"

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.