The use of QT sql

Source: Internet
Author: User
Tags prepare qt designer

Tag: Test Designer implementation failed Oracle ETH long sqli 127.0.0.1

The SQL class is divided into three tiers:

    • user interface layer: These classes provide data-based widgets that connect to a database and are browsed by users. The QT Designer integrates these classes and can be used to create data-based forms. These widgets can interact directly with C + + code in your program. The classes that support this layer include: Qsqleditorfactory, Qsqlform, Qsqlpropertymap, Qdatatable, Qdatabrowser, and Qdataview.
    • SQL application Programming Interface layer: These classes access the database. The Qsqldatabase class is used to connect to the database, and the implementation of the data interaction either uses the Qsqlquery class as an SQL statement or the Qsqlcursor class, which encapsulates the set of SQL commands. In addition to Qsqldatabase, Qsqlcursor and qsqlquery These classes, Qsqlerror, Qsqlfield, Qsqlindex, and Qsqlrecord also support this layer.
    • driver Layer: This layer consists of three classes: Qsqlresult, Qsqldriver and Qsqldriverfactoryinterface. This layer provides the underlying bridge between the database and the SQL class. The documentation for this layer is additional because it is only relevant to driver writers and is seldom used in standard database application programming.

To use SQL in QT, you first need to include the SQL module:

QT       += core gui sql

You can include the header file you want to use first:

#include <QSqlDatabase>#include <QDebug>#include <QMessageBox>#include <QSqlError>#include <QSqlQuery>

Use Qsqldata::d rivers () to print QT-supported database drivers:

qDebug() << QSqlDatabase::drivers();

The currently supported databases are as follows:

Qt: Untested Windows version 10.0 detected!("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")

We are using MySQL database, so we will add MySQL database first:

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");

Then connect to the local database, this table needs to build itself, if there is no database on the local pop up a messagebox, prompted to open the database failure:

//连接数据库    db.setHostName("127.0.0.1");   //数据库服务器ip    db.setUserName("root");        //数据库用户名    db.setPassword("123456");      //密码    db.setDatabaseName("info");    //使用哪个数据库    //打开数据库    if(!db.open())    {        QMessageBox::warning(this, "错误", db.lastError().text());        return;    }

Inserting data into a database we use the Qsqlquery class, which supports MySQL statements:

 //插入QSqlQuery query;query.exec("");  //sql语句
Add (Insert)

Inserting data into a database we have different styles:

    1. ODBC style
//批量插入//? 相当于点位符query.prepare("insert into student(name, age, score) value(?, ?, ?)");//给字段设置内容 listQVariantList nameList;nameList << "xiaoming" << "xiaolong" << "xiaojiang";QVariantList ageList;ageList << 11 << 22 << 33;QVariantList scoreList;scoreList << 59 << 69 << 79;//给字段绑定相应的值 按顺序绑定query.addBindValue(nameList);query.addBindValue(ageList);query.addBindValue(scoreList);//执行预处理命令query.execBatch();
    1. Oracle Style
//占位符 : + 自定义名字query.prepare("insert into student(name, age, score) value(:name, :age, :score)");//给字段设置内容 listQVariantList nameList;nameList << "xiaom" << "xiao" << "xiang";QVariantList ageList;ageList << 11 << 22 << 33;QVariantList scoreList;scoreList << 89 << 91 << 99;//给字段绑定query.bindValue(":name", nameList);query.bindValue(":score", scoreList);query.bindValue(":age", ageList);//执行预处理命令query.execBatch();
Check
//查query.exec("select * from student");while(query.next()) //一行一行遍历{    //取出当前行的内容    //以列为单位的    query.value(0).toInt();      //第0列    query.value(1).toString();    query.value("age").toInt();  //直接放字段名字也可以    query.value("score").toInt();}

Cond..............

The use of QT sql

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.