Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

qt - Why there is no prepare statement in mysql after I calling QSqlQuery::prepare function

Here are my test codes:


    //Get a database connection of type MySQL from my function.

    auto pdatabase = INDBPOOL->GetConnection();
    if (pdatabase == Q_NULLPTR)
        return;

    QString sql = "select * from task where task.taskId = ? and task.isDeleted = 0";

    //p_query is a member of class database connection. It's QSqlQuery class.
    bool ok = pdatabase->p_query->prepare(sql);
    if (!ok) {
        qDebug() << "prepare error!";
        return;
    }
    pdatabase->p_query->bindValue(0,1);
    pdatabase->p_query->exec();
    if (!pdatabase->p_query->isActive()) {
        qDebug() << "exec error";
    }

    pdatabase->p_query->exec("SELECT * FROM `performance_schema`.prepared_statements_instances");
    if (!pdatabase->p_query->isActive()) {
        qDebug() << "exec error";
    }

    while (pdatabase->p_query->next()) {
        qDebug() << pdatabase->p_query->value("SQL_TEXT").toString();
        qDebug() << pdatabase->p_query->value("STATEMENT_NAME").toString();
        qDebug() << pdatabase->p_query->value("COUNT_EXECUTE").toInt();
    }

    INDBPOOL->ReleaseConnection(pdatabase);
    return;
}

I can guarantee that all of the sql statements in the codes run successfully, but there is no data in the table performance_schema.prepared_statements_instances. That means there is no prepared statements in mysql database after I calling function prepare of class QSqlQuery. My Qt version is 5.12.9 and MySQL database version is 8.0. I don't know why. Who Knows the detail please tell me. Thank you very much!

question from:https://stackoverflow.com/questions/65937551/why-there-is-no-prepare-statement-in-mysql-after-i-calling-qsqlqueryprepare-fu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Class QSqlQuery prepares or executes a statement, it will deallocate prepared statement that the query created previously first. So when I using the same database connection to search the prepared statement info in table performance_schema.prepared_statements_instances, it has been deleted already.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...