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. Copies of QSqlQuery share the same bound values
Forum Updated to NodeBB v4.3 + New Features

Copies of QSqlQuery share the same bound values

Scheduled Pinned Locked Moved Solved General and Desktop
qsqlquerycopying
7 Posts 3 Posters 1.1k Views 1 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.
  • S Offline
    S Offline
    Skogach
    wrote on 13 Mar 2021, 07:45 last edited by Skogach
    #1

    When I try to copy prepared QSqlQuery with assign or copy constructor and bind to them different values with bindValue they both will have the same bound value, one which was bound last. Is it the case of implicit sharing? Though this class is not listed as implicitly shared. I wasn't able to find anything in docs. What should I do to make independent copies of a query? I'm using Qt 5.13.2. Here is the test code:

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "test");
    db.setDatabaseName("text.sqlite3");
    db.open();
    QSqlQuery createQuery(db);
    createQuery.exec("CREATE TABLE if not exists test"
                                              "(test_id integer)");
    QSqlQuery query1(db);
    query1.prepare("INSERT INTO test (test_id) VALUES(:test_id)");
    query1.bindValue(":test_id", 1);
    qDebug() << query1.boundValue(":test_id"); //this returns 1
    QSqlQuery query2(query1); //the same with QSqlQuery query2 = query1;
    query2.bindValue(":test_id", 2);
    qDebug() << query1.boundValue(":test_id") << query2.boundValue(":test_id"); //both return 2
    
    K 1 Reply Last reply 13 Mar 2021, 07:52
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 13 Mar 2021, 09:14 last edited by
      #5

      Create a bug report, with a proper minimal compilable example (use SQLite in-memory db) and assign it to me. For now simply use two distinct queries and prepare both.

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

      S 1 Reply Last reply 13 Mar 2021, 09:46
      2
      • S Skogach
        13 Mar 2021, 07:45

        When I try to copy prepared QSqlQuery with assign or copy constructor and bind to them different values with bindValue they both will have the same bound value, one which was bound last. Is it the case of implicit sharing? Though this class is not listed as implicitly shared. I wasn't able to find anything in docs. What should I do to make independent copies of a query? I'm using Qt 5.13.2. Here is the test code:

        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "test");
        db.setDatabaseName("text.sqlite3");
        db.open();
        QSqlQuery createQuery(db);
        createQuery.exec("CREATE TABLE if not exists test"
                                                  "(test_id integer)");
        QSqlQuery query1(db);
        query1.prepare("INSERT INTO test (test_id) VALUES(:test_id)");
        query1.bindValue(":test_id", 1);
        qDebug() << query1.boundValue(":test_id"); //this returns 1
        QSqlQuery query2(query1); //the same with QSqlQuery query2 = query1;
        query2.bindValue(":test_id", 2);
        qDebug() << query1.boundValue(":test_id") << query2.boundValue(":test_id"); //both return 2
        
        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 13 Mar 2021, 07:52 last edited by
        #2

        Call QSqlQuery::clear as appropriate.

        Read and abide by the Qt Code of Conduct

        S 1 Reply Last reply 13 Mar 2021, 08:02
        0
        • K kshegunov
          13 Mar 2021, 07:52

          Call QSqlQuery::clear as appropriate.

          S Offline
          S Offline
          Skogach
          wrote on 13 Mar 2021, 08:02 last edited by
          #3

          @kshegunov Which query should I clear? As I understand, clear will remove prepared statement from query. But I need to get two prepared queries with different bound values.

          K 1 Reply Last reply 13 Mar 2021, 08:09
          0
          • S Skogach
            13 Mar 2021, 08:02

            @kshegunov Which query should I clear? As I understand, clear will remove prepared statement from query. But I need to get two prepared queries with different bound values.

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 13 Mar 2021, 08:09 last edited by kshegunov
            #4

            @Skogach said in Copies of QSqlQuery share the same bound values:

            @kshegunov Which query should I clear? As I understand, clear will remove prepared statement from query. But I need to get two prepared queries with different bound values.

            Yes that's correct and I'm sorry I just looked more carefully at the code. You should file a bug report if one isn't already in the tracker, this is not how it's intended to work. The copy constructor should create a copy, not keep an internal reference to the other query. As a workaround I'd just prepare the second query separately.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 13 Mar 2021, 09:14 last edited by
              #5

              Create a bug report, with a proper minimal compilable example (use SQLite in-memory db) and assign it to me. For now simply use two distinct queries and prepare both.

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

              S 1 Reply Last reply 13 Mar 2021, 09:46
              2
              • C Christian Ehrlicher
                13 Mar 2021, 09:14

                Create a bug report, with a proper minimal compilable example (use SQLite in-memory db) and assign it to me. For now simply use two distinct queries and prepare both.

                S Offline
                S Offline
                Skogach
                wrote on 13 Mar 2021, 09:46 last edited by
                #6

                @Christian-Ehrlicher I created bug report , but I couldn't find how to assign it, so I mentioned you in comments.

                1 Reply Last reply
                1
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 13 Mar 2021, 11:30 last edited by
                  #7

                  Thx. Will take a look on it and hope it will go into 5.15 (even we as opensource user currently don't have an advantage currently but this hopefully changes somehow in the near future)

                  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
                  1

                  1/7

                  13 Mar 2021, 07:45

                  • Login

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