Example of QT establishing database connection, adding, deleting, modifying and checking

The following are mainly operations on the mysql database, other database operations are actually similar:

 

1. Add QT+= sql to the .pro file, as shown below:



2. Add SQL related header files

#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>

3. Connect to the database

    qDebug()<<QSqlDatabase::drivers();    //查询支持的sql对象,下面调用的数据库实例QMYSQL就是从这里的结果确定的

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); //数据库对象

    //连接数据库
    db.setHostName("192.168.0.1");            //数据库服务器IP
    db.setPort(3306);                        //端口
    db.setUserName("test");                 //数据库用户名
    db.setPassword("123456");               //数据库密码
    db.setDatabaseName("test");              //使用哪个数据库

    if( !db.open() ){       //数据库打开失败
        QMessageBox::warning(this,"错误",db.lastError().text());
        return;
    }

4. Query data
We put the found data into a tablewidget, which looks intuitive:

    QStringList headerText;
    headerText<<"权限"<<"姓名"<<"密码";
    ui->tableWidget->setColumnCount(headerText.count());

    for(int i = 0;i<headerText.count();i++)
    {
        ui->tableWidget->setHorizontalHeaderItem(i,new QTableWidgetItem(headerText.at(i)));
    }

    QSqlQuery query;    //查询语句
    query.exec("select * from user;");

    qint32 rowNo = 0;

    while(query.next())
    {
        QTableWidgetItem *item;
        QStandardItem *sItem;

        qDebug()<<query.value(0).toString()<<":"<<query.value(1).toString()<<":"<<query.value(2).toString()<<":"<<query.value(3).toString();
        ui->tableWidget->insertRow(rowNo);

        for(int i=0;i<3;i++)
        {
             item = new  QTableWidgetItem(query.value(i+1).toString(),i);
             ui->tableWidget->setItem(rowNo,i,item);
        }
        rowNo++;
    }

You can see the query results:




5. New data
Just call the query method of QSqlQuery. For query errors, you can use its lastError function to read the reason for the command failure.

    QSqlQuery query;

    QString sqls = QString("insert into user values(%1,%2,%3,%4);")
            .arg(11).arg(2).arg("'test1'").arg("'test1'");

    qDebug()<<sqls;

    if(query.exec(sqls))
    {
        qDebug()<<"成功";
    }
    else
    {
        qDebug()<<"失败";
        qDebug()<<query.lastError();
    }

6. Delete data

    QSqlQuery query;

    //删除ID=11的用户
    QString sqls = QString("delete from user where id = %1").arg(11);

    qDebug()<<sqls;

    if(query.exec(sqls))
    {
        qDebug()<<"成功";
    }
    else
    {
        qDebug()<<"失败";
        qDebug()<<query.lastError();
    }

7. Modify the data

     QString sqls = QString("update user set username = %1 where id = %1;")
             .arg("'user1'").arg(10);

    qDebug()<<sqls;

    if(query.exec(sqls))
    {
        qDebug()<<"成功";
    }
    else
    {
        qDebug()<<"失败";
        qDebug()<<query.lastError();
    }

Other operations, such as table operations, are all sql filling and splicing issues, so I won’t describe them more.

8. When Qt connects to MySQL, it prompts "QSqlDatabase: QMYSQL driver not loaded". It may be that the library file libmysql.dll is missing. You can find this file from the installation directory of msyql and place it in the bin directory under the compiler directory of the QT installation directory. Below, such as "C:\Qt\Qt5.9.0\5.9\mingw53_32\bin"
If this still does not work, check whether the digits of the installed mysql are the same as that of QT, if not, go to this address:
https://dev.mysql .com/downloads/connector/c/
Download mysql-connector-c, find the file "libmysql.dll" in its lib directory, copy this file to Qt's bin directory, restart Qt Creator and it will run correctly.
In short, make sure that the mysql bit number corresponding to the libmysql.dll file under the program running environment is the same as the QT bit number.

Guess you like

Origin blog.csdn.net/zhangfls/article/details/114279217