Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    3
    15
    1090
    Loading More Posts
    • 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.
    • P
      PetrS82 last edited by

      Hello,
      I have following problem. I need to bind values in more location than one and it doesn't work.
      In QT 4.8 documentation is note that

      "Values cannot be bound to multiple locations in the query, eg:
      INSERT INTO testtable (id, name, samename) VALUES (:id, :name, :name)
      Binding to name will bind to the first :name, but not the second."

      But in the documentation for newer versions of QT this note does not appear. Is it because it is fixed there?
      But why it does not work in my Qt 5.10.1 ?

      Thank you for the answer.

      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        It's still not possible

        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 Reply Quote 2
        • P
          PetrS82 last edited by

          I' sorry exactly this example ( INSERT INTO testtable (id, name, samename) VALUES (:id, :name, :name) ) works.
          But some exaples deos not work.

          1 Reply Last reply Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion last edited by

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

            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 Reply Quote 1
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              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.

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

              P 1 Reply Last reply Reply Quote 0
              • P
                PetrS82 @SGaist last edited by

                Oracle 12.1.0

                1 Reply Last reply Reply Quote 0
                • P
                  PetrS82 @Christian Ehrlicher last edited by PetrS82

                  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 Reply Quote 0
                  • Christian Ehrlicher
                    Christian Ehrlicher Lifetime Qt Champion last edited by

                    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 Reply Quote 0
                    • P
                      PetrS82 @Christian Ehrlicher last edited by

                      This post is deleted!
                      1 Reply Last reply Reply Quote 0
                      • P
                        PetrS82 @Christian Ehrlicher last edited by

                        @Christian-Ehrlicher QOCI

                        1 Reply Last reply Reply Quote 0
                        • P
                          PetrS82 @Christian Ehrlicher last edited by

                          @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 Reply Quote 0
                          • Christian Ehrlicher
                            Christian Ehrlicher Lifetime Qt Champion last edited by

                            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 Reply Quote 0
                            • SGaist
                              SGaist Lifetime Qt Champion last edited by

                              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 Reply Quote 0
                              • P
                                PetrS82 @Christian Ehrlicher last edited by

                                @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 Reply Quote 0
                                • SGaist
                                  SGaist Lifetime Qt Champion last edited by

                                  @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 Reply Quote 1
                                  • First post
                                    Last post