Qt operation sqlserver database

Because the database experiment operation stipulates that a visual interface must be done, the fish monster has to continue Qt (;´༎ຶД༎ຶ`)

Ready to work

The preparation here is to build the database, build the table, and then configure the ODBC data source in the control panel

The preparation content is the same as the previously written VS2017 operation sql server database preparation content (except for the code operation part)

This article also lists the main code of the operation according to the example of VS2017 operating the sql server database

Code implementation part

  1. First, you need to import it in the .pro file in the newly created Qt project: QT =+ sql

  2. Connect to the database

    #include "mainwindow.h"
    #include <QApplication>
    #include <QDebug>
    #include <QMessageBox>
    #include <QSqlError>
    #include <QSqlDatabase>
    #include <QSqlQuery>
    
    void load_Database()
    {
        QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");//数据库驱动类型位sqlserver 
    	db.setHostName("localhost");        //选择本地主机,或则127.0.0.1
    	db.setDatabaseName("haha");       //设置数据库名称
    	db.setUserName("用户名");       //登录用户
    	db.setPassword("密码");     //密码
    	if(!db.open())      //打开数据库失败
    	{
    		qDebug()<<db.lastError().text();
        	QMessageBox::critical(0,QObject::tr("Database error"),db.lastError().text());
    	}
    	else
    	{
    		qDebug()<<"database open success!";
    	}
    }
    

Note: The user name and password should be changed to your own user and password. The last example database "haha" used in this article continues, remember to change to your own!

  1. Insert data

    void insert_value()
    {
        QString str = QString("insert into student values(%1, '%2', %3)").arg(666).arg("宝儿姐").arg(20);
        QSqlQuery query;
        query.exec(str);
    }
    

    Insert a row of data into the student table of the "haha" database, or use the prepare() function to insert data into the database:

    void insert_value_1()
    {    QSqlQuery query;
        query.prepare("insert into student(ID,name,age) values(:ID,:name,:age)");
        query.bindValue(":ID",666666);		//按照标识符绑定值
        query.bindValue(":name","阿尔托莉雅");
        query.bindValue(":age",20);
        query.exec();
    }
    
  2. Query data

    void search_value()
    {
        QSqlQuery query;
        query.exec("select * from student");
        while(query.next())	//循环输出
        {
            qDebug()<<query.value(0).toInt()<<query.value(1).toString()<<query.value(2).toInt();
        }
    }
    

    Query all data in student:

  3. change the data

    void update_value()
    {
        QString str = QString("update student set name = '%1' where ID = %2").arg("zero-two").arg(666);
        QSqlQuery query;
        query.exec(str);
    }
    

    Update the data to modify the name with ID 666 to "zreo-two";

  4. delete data

    void delete_value()
    {
        QString str = QString("delete from student where ID = %1").arg(666);
        QSqlQuery query;
        query.exec(str);
    }
    

    Delete the data with ID 666:

Modifying, querying, and deleting can also be implemented with the prepare() function in QSqlQuery. The steps are the same as the second method of inserting data, just modify the SQL statement. The function of adding, deleting, modifying and checking has been realized, after that, the data information is obtained with the controls that come with Qt, and then the visualization function is further realized.

Guess you like

Origin blog.csdn.net/xwmrqqq/article/details/106198090