QT Development (46)--QT Database programming Basics

Source: Internet
Author: User
Tags ibm db2 odbc sqlite postgresql version

QT Development (46)--the basis ofQT database programmingFirst, Introduction to the Qt SQL module 1. Qt   S QL Module Introduction

QT provides support for SQL database through QT SQL Module , andthe API in QT SQL module is divided into three layers: drive layer,SQL interface layer, user interface layer.

If you want to use Classes in the QT SQL module require the QT + = SQL code to be added to the project file (. Pro file) .

2. Drive Layer

the drive layer is for specific databases and The SQL interface layer provides the underlying bridge, the main classes include the Qt SQL module qsqldriver,qsqldrivercreator, Qsqldrivercreatorbase,qsqldriverplugin and qsqlresult.

3. SQL interface layer

The SQL interface layer provides access to the database, including the Qsqldatabase,qsqlquery,qsqlerror, and the main classes in the Qt SQL module. Qsqlfield,qsqlindex and Qsqlrecord. The Qsqldatabase class is used to create a database connection,and Qsqlquery is used to interact with the database using SQL statements.

4. User interface Layer

The user interface layer mainly includes Qsqlquerymodel,qsqltablemodel,Qsqlrelationaltablemodel in the Qt SQL module. The class of the user interface layer implements linking the data in the database to the widgets, is implemented using the Model /view framework, and is a higher level of abstraction that can manipulate the database even if you are unfamiliar with SQL . It is important to note that the Qcoreapplication object must be instantiated before using the class of the user interface layer .

Second, Database-driven

The Qt SQL module communicates using a database driver plug-in and a different database interface. Since QT's SQL Module interfaces are database-independent, the code for all specific databases is included in these drivers. QT itself provides a variety of database drivers, and additional database drivers can be added. The database-driven source code provided by QT can be used as a model for writing custom drivers.

The database drivers supported by the QT5.7 version are as follows:

Database-driven

Note

QDB2

IBM DB2 7.1 and later

Qibase

Borland InterBase

Qmysql

Mysql

Qoci

Oracle Call Interface Driver

Qodbc

Odbc

Qpsql

PostgreSQL version 7.3 and above

QSQLITE2

SQLite Version 2

Qsqlite

SQLite version 3

Qtds

Sybase Adaptive SERVER,QT 4.7 started scrapping

The database drivers supported by the QT5.7 version are as follows:

because Compatibility issues with GPL licenses, not all of the driver plugins listed are provided to the open source version of Qt. actual The drivers installed in Qt can be displayed according to the following code:

#include <QCoreApplication> #include <QSqlDatabase> #include <QDebug> #include <QStringList>    int main (int argc, char *argv[]) {qcoreapplication A (argc, argv);    Qdebug () << "Available drivers:";    Qstringlist drivers = qsqldatabase::d rivers ();    foreach (QString driver, drivers) qdebug () << driver; return a.exec ();}
third, database connection 1. Connect MySQL database

Establish a database connection using MySQL Database driver

Qsqldatabase db = Qsqldatabase::adddatabase ("Qmysql");

Db.sethostname ("IP");// Set host

Db.setdatabasename ("dbname"); Setting the database name

Db.setusername ("user"); Set user name

Db.setpassword ("password"); Set Password

bool OK = Db.open (); connecting to a database

2. connecting the Acess database

Connect to a database using ODBC driver

Qsqldatabase LDB = qsqldatabase::adddatabase ("Qodbc");

Ldb.setdatabasename ("Driver={microsoft access DRIVER (*.mdb)};fil={ms access};D Bq=db.mdb; Uid=user; Pwd=passwor ");

bool OK = Ldb.open ();

3. connecting to a SQL Server database

Qsqldatabase db = Qsqldatabase::adddatabase ("Qodbc");

Db.setdatabasename (QString ("Driver={sql SERVER}; server=%1; database=%2; uid=%3; pwd=%4; "). Arg ("IP"). Arg ("dbname"). Arg ("user"). Arg ("password"));

bool OK = Db.open ();

4. Connect to SQLite database

Establish a database connection using SQLite Database driver

Qsqldatabase db = Qsqldatabase::adddatabase ("Qsqlite");

Db.sethostname ("IP");// Set host

Db.setdatabasename ("dbname"); Setting the database name

Db.setusername ("user"); Set user name

Db.setpassword ("password"); Set Password

bool OK = Db.open (); connecting to a database



This article from "Life is endless, struggle not only" blog, declined reprint!

QT Development (46)--QT Database programming Basics

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.