Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

    Hi
    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?


  • Qt Champions 2019

    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):

    main.cpp:

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

    MainWindow.h:

    #include <QMainWindow>
    
    class MainWindow : public QMainWindow
    {
    
    public:
        MainWindow();
        
    };
    

    MainWindow.cpp:

    #include "MainWindow.h"
    
    #include <QSqlDatabase>
    #include <QSqlQuery>
    #include <QDebug>
    
    MainWindow::MainWindow()
    {
        QSqlDatabase db;
        db = QSqlDatabase::addDatabase(QString::fromUtf8("QSQLITE"), QString::fromUtf8("SQLiteDB"));
        db.setDatabaseName(QString::fromUtf8("test.db"));
        db.open();
    
        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"));
        query.exec();
    
        qDebug() << query.executedQuery();
    
        db.close();
    }
    

    Output (notice the unsubstituted "?" characters):

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

    Control:

    $ sqlite3 test.db "SELECT * FROM config"
    foo|bar

Log in to reply