qt操作mysql进行查询出现错误如何解决

代码如下:
mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //打印qt支持的数据库驱动
    qDebug() << QSqlDatabase::drivers();

    //添加MySql数据库
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    //连接数据库
    db.setHostName("127.0.0.1");  //数据库服务器IP
    db.setUserName("root");    //数据库用户名
    db.setPassword("123456");  //密码
    db.setDatabaseName("test");  //使用哪个数据库
    //打开数据库
    if(db.open() == false)
    {
        QMessageBox::warning(this, "错误", db.lastError().text());
        return;
    }
    //操作sql语句
    QSqlQuery query;
    query.exec("create table if not exists student(id int primary key, name varchar(255), age int, score int);");
    //query.exec("insert into student(id, name, age, score) values(1, 'mike', 18, 59);");
    query.exec("select * from student");
    while(true == query.next())     //一行一行遍历
    {
        //取出当前行的内容,以列为单位
        qDebug() << query.value(0).toInt()  //取第一列
                 << query.value(1).toString() //取第二列
                 << query.value("age").toInt()  //按名字查找
                 << query.value("score").toInt();
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

报错:

img

测试发现,这个问题似乎只在查询后才会出现,