Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlQuery::executedQuery() does not return a prepared query with SQLite
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • l3u_L Offline
    l3u_L Offline
    l3u_
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      l3u_L 1 Reply Last reply
      1
      • l3u_L l3u_

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @l3u_ said in QSqlQuery::executedQuery() does not return a prepared query with SQLite:

        QSqlQuery::executedQuery

        http://www.qtcentre.org/threads/57676-QSqlQuery-executedQuery-()-Bound-values-versus-place-holders

        l3u_L 1 Reply Last reply
        0
        • mrjjM mrjj

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

          l3u_L Offline
          l3u_L Offline
          l3u_
          wrote on last edited by
          #4

          @mrjj It did!

          1 Reply Last reply
          0
          • JonBJ JonB

            @l3u_ said in QSqlQuery::executedQuery() does not return a prepared query with SQLite:

            QSqlQuery::executedQuery

            http://www.qtcentre.org/threads/57676-QSqlQuery-executedQuery-()-Bound-values-versus-place-holders

            l3u_L Offline
            l3u_L Offline
            l3u_
            wrote on last edited by
            #5

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

            JonBJ 1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • l3u_L l3u_

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @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.

                1 Reply Last reply
                0
                • l3u_L Offline
                  l3u_L Offline
                  l3u_
                  wrote on last edited by l3u_
                  #8

                  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
                  
                  1 Reply Last reply
                  2

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved