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::bindValue() : Is possible to bound values to multiple locations?
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery::bindValue() : Is possible to bound values to multiple locations?

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 3 Posters 2.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.
  • SGaistS SGaist

    Hi,

    What SQL backend are you using ?
    Some have changed over time to implement that feature. IIRC, the SQLite driver recently was updated because the SQLite version used had that feature implemented.

    P Offline
    P Offline
    PetrS82
    wrote on last edited by
    #6

    Oracle 12.1.0

    1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      @PetrS82 said in QSqlQuery::bindValue() : Is possible to bound values to multiple locations?:

      But some exaples deos not work.

      Which example does not work? 'Some' is a little bit imprecise.

      P Offline
      P Offline
      PetrS82
      wrote on last edited by PetrS82
      #7

      Hello,
      I made following table (using Oracle SQL Developer):
      create table testtable (KEY VARCHAR(255), VALUE VARCHAR(255), ID VARCHAR(255));

      In C++ I have folloving code:

      QSqlQuery query(db);
      
      query.prepare("declare "
                  "x NUMBER; "
                  "begin "
                  "insert into testtable(KEY, VALUE, ID) "
                  "values ('INIT_TEST', :INIT_ID, :INIT_ID); "
                  "insert INTO testtable(ID, KEY, VALUE) "
                  "select :RESULT_ID, 'TEST_INIT_ID', VALUE "
                  "from testtable "
                  "where ID = :INIT_ID; "
                  "end; ");
      
      
      query.bindValue(":INIT_ID", 1000);
      query.bindValue(":RESULT_ID", 2000);
      
      
      if (query.exec()) {
          cout << "query.exec: OK" << endl;
      } else {
          cout << "query.exec: error " << query.lastError().text().toStdString() << endl;
      }
      

      The expected result is:

       ID     |       KEY         |  VALUE
      1000    |   INIT_TEST       |  1000
      2000    |  TEST_INIT_ID     |  1000
      

      But the real result is:

       ID     |        KEY       |  VALUE
      1000    |   INIT_TEST      |  1000
      1000    |  TEST_INIT_ID    |  1000
      

      Both IDs are 1000 (and console output is "query.exec: OK").
      Note that if I run this statement in SQL Developer, the result is as expected.

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

        As @SGaist and I already said - don't use it. It may work (with the Qt Sqlite driver) but obviously not with the Qt ODBC driver (which you seem to use, but did not yet told us).

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

        P 3 Replies Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          As @SGaist and I already said - don't use it. It may work (with the Qt Sqlite driver) but obviously not with the Qt ODBC driver (which you seem to use, but did not yet told us).

          P Offline
          P Offline
          PetrS82
          wrote on last edited by
          #9
          This post is deleted!
          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            As @SGaist and I already said - don't use it. It may work (with the Qt Sqlite driver) but obviously not with the Qt ODBC driver (which you seem to use, but did not yet told us).

            P Offline
            P Offline
            PetrS82
            wrote on last edited by
            #10

            @Christian-Ehrlicher QOCI

            1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              As @SGaist and I already said - don't use it. It may work (with the Qt Sqlite driver) but obviously not with the Qt ODBC driver (which you seem to use, but did not yet told us).

              P Offline
              P Offline
              PetrS82
              wrote on last edited by
              #11

              @Christian-Ehrlicher
              Is QOCI what you asked me? I don't know more. Here is one line (edited) form config.xml file we use for connection

              connection user="user_foo" password="password_foo" host="localhost" port="port_number" typ="QOCI" name="DATABASE_FOO" sid="ORCL" db_name="ORCL"

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

                QOCI is even older - it has not been touched since Qt4 times so if it did not work with Qt4 it will not with Qt5.

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

                P 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #13

                  IIRC, you can check if the driver provided that feature.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    QOCI is even older - it has not been touched since Qt4 times so if it did not work with Qt4 it will not with Qt5.

                    P Offline
                    P Offline
                    PetrS82
                    wrote on last edited by
                    #14

                    @Christian-Ehrlicher

                    cout << "NamedPlaceholders: " << db.driver()->hasFeature(QSqlDriver::NamedPlaceholders) << endl;
                    cout << "PositionalPlaceholders: " << db.driver()->hasFeature(QSqlDriver::PositionalPlaceholders) << endl;

                    OUTPUT:
                    NamedPlaceholders: 1
                    PositionalPlaceholders: 0

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #15

                      @PetrS82 said in QSqlQuery::bindValue() : Is possible to bound values to multiple locations?:

                      query.prepare("declare "
                      "x NUMBER; "
                      "begin "
                      "insert into testtable(KEY, VALUE, ID) "
                      "values ('INIT_TEST', :INIT_ID, :INIT_ID); "
                      "insert INTO testtable(ID, KEY, VALUE) "
                      "select :RESULT_ID, 'TEST_INIT_ID', VALUE "
                      "from testtable "
                      "where ID = :INIT_ID; "
                      "end; ");

                      Isn't that set of queries invalid ?
                      Your second insert is missing the values part and your select clause explicitly asks to show the value of :RESULT_ID first and then the content of the other two columns. Which makes the output you show even more surprising.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1

                      • Login

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