Qt connects to MySQL database and operates visually with Navicat

0. Download MySQL and Navicat

Register the MySQL account password, which will be used later.

1. Check the database driver supported by Qt

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

2. Check that the number of bits in Qt corresponds to the number of bits in MySQL. 32-bit corresponds to 32-bit.

Copy the MySQL driver libmysql.dll library to the corresponding debug directory or release directory.Insert image description here
Insert image description here

//添加一个数据库
    QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");    //括号内要写出数据库的类型
    //设置数据库
    db.setHostName("127.0.0.1"); //设置数据库的主机ip,localhost也可以
    //设置端口,mysql的端口默认是3306
    db.setPort(3306);
    //设置数据库的用户名
    db.setUserName("root");
    //设置数据库的密码
    db.setPassword("123456");    //这个就是安装MySQL时设置的密码
    //设置数据库的名字
    db.setDatabaseName("aaa2");
    //打开数据库(已经安装过mysql驱动了)
    if(db.open()==false){
    
    
        QMessageBox::warning(this,"waring",db.lastError().text());
    }
    else	QMessageBox::about(this,"success","数据库打开成功");

Run successfully

Insert image description here

Cascading operations on primary and foreign keys

Create two new tables in Navicat. I built the student table and the class information table of classinfo. In the class information table, classID is set as the primary key and the type is int. A field is added to the student table and the class information table is added. The fields classID and names do not have to be the same, just follow your own habits to facilitate distinction.


student's student table

Insert image description here

Insert a foreign key into the student table, choose the name yourself, and select the classID in the student table as the field. The referenced schema is your current database, the referenced parent table is the main table you want to associate, and the referenced field is the parent table. For a certain field in the table, select CASCADE (cascade mode) when deleting and updating. When the associated fields in the parent table are updated, the corresponding fields in the slave table will also be updated.

When inserting a foreign key, you must carefully check whether the fields associated with the master table and the slave table match. For example, type, length, decimal point, not null, and virtual must match one by one. Otherwise, an error will be reported and the foreign key cannot be established.
,
Insert image description here
add some data to the table and perform operations
Insert image description here

Insert image description here

After deleting the associated field records in the master table, the data in the slave table will be updated. Here we delete the record of 1 in classID, and refresh the student table and find that the record of classID has also been deleted. This is the basic cascade operation. Of course, it can also be associated with multiple tables, which may be a bit more complicated.
Insert image description here

Guess you like

Origin blog.csdn.net/qq_52531759/article/details/130137285