Congratulations to our 2022 Qt Champions!

QSqlQuery::executedQuery() does not return a prepared query with SQLite

  • Hi :-)

    I use QSqlQuery::executedQuery() for debugging purposes. The docs say:

    The placeholders in the original query are replaced with their bound values to form a new query. This function returns the modified query.

    If I understand this correctly, a query like UPDATE config SET value = ? WHERE key = ? with foo and bar as bound values should be returned as UPDATE config SET value = 'foo' WHERE key = 'bar' or similar, shouldn't it? Instead, I always get the "original" query with the ? characters unchanged.

    Am I missing something or is this a bug? I use Qt 5.9.4.

  • Lifetime Qt Champion

    Might be a silly question, but it did execute correctly ?

  • @mrjj It did!

  • @JonB Thanks for the link! The initial post asks the same question as I do here – but they talk about replication and inserting queries into another database later on, so I think the question (bug or feature or misread docs) is not answered there. Or did I miss something there?

  • Lifetime Qt Champion

    Please provide a minimal, compilable example so we can reproduce the issue. There are many things which can go wrong here...

  • @l3u_
    Yes, I think I quoted it just for its explanation of what the behaviour should be under what circumstances.

    So, just to be 100% clear because I don't see you've stated it above explicitly: when do you examine QSqlQuery::executedQuery() in your code? It only claims to do the substitution (if at all) after it has executed the query.

  • Here we go. I took a modified "Hello world" program to make clear what I mean (assuming there's no "test.db" file where the program is executed):


    #include "MainWindow.h"
    #include <QApplication>
    int main(int argc, char* argv[])
        QApplication app(argc, argv);
        MainWindow mainWindow;;
        return app.exec();


    #include <QMainWindow>
    class MainWindow : public QMainWindow


    #include "MainWindow.h"
    #include <QSqlDatabase>
    #include <QSqlQuery>
    #include <QDebug>
        QSqlDatabase db;
        db = QSqlDatabase::addDatabase(QString::fromUtf8("QSQLITE"), QString::fromUtf8("SQLiteDB"));
        QSqlQuery query(db);
        query.exec(QString::fromUtf8("CREATE TABLE config(key TEXT, value TEXT)"));
        query.prepare(QString::fromUtf8("INSERT INTO config(key, value) VALUES(?, ?)"));
        query.bindValue(0, QString::fromUtf8("foo"));
        query.bindValue(1, QString::fromUtf8("bar"));
        qDebug() << query.executedQuery();

    Output (notice the unsubstituted "?" characters):

    "INSERT INTO config(key, value) VALUES(?, ?)"


    $ sqlite3 test.db "SELECT * FROM config"

Log in to reply