Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. how to select where xxx=? sqlite c++
QtWS25 Last Chance

how to select where xxx=? sqlite c++

Scheduled Pinned Locked Moved Solved 3rd Party Software
5 Posts 2 Posters 3.9k Views
  • 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.
  • F Offline
    F Offline
    filipdns
    wrote on 5 Feb 2018, 13:55 last edited by
    #1

    Hi,

    I used sqlite query in js and all is working but in c++ I can't do same, could you help?

    In js:

    function dbRead(yyyy)
    {
        var db = dbGetHandle()
        db.transaction ( function (tx) {
            var results = tx.executeSql(
    'SELECT * FROM table WHERE columnx = ?', [yyyy] )
       })
    }
    

    in cpp I have:

            QSqlQuery query;
            query.exec("SELECT * FROM table WHERE columnx = 'yyyy' " );
    

    how can I replace 'yyyy' by variable string in C++?

    thank you very much for your help

    Philippe

    J 1 Reply Last reply 5 Feb 2018, 14:11
    0
    • F filipdns
      5 Feb 2018, 13:55

      Hi,

      I used sqlite query in js and all is working but in c++ I can't do same, could you help?

      In js:

      function dbRead(yyyy)
      {
          var db = dbGetHandle()
          db.transaction ( function (tx) {
              var results = tx.executeSql(
      'SELECT * FROM table WHERE columnx = ?', [yyyy] )
         })
      }
      

      in cpp I have:

              QSqlQuery query;
              query.exec("SELECT * FROM table WHERE columnx = 'yyyy' " );
      

      how can I replace 'yyyy' by variable string in C++?

      thank you very much for your help

      Philippe

      J Offline
      J Offline
      JonB
      wrote on 5 Feb 2018, 14:11 last edited by JonB 2 May 2018, 14:11
      #2

      @filipdns
      See http://doc.qt.io/qt-5/sql-sqlstatements.html, e.g.

          QSqlQuery query;
          query.prepare("INSERT INTO employee (id, name, salary) "
                        "VALUES (?, ?, ?)");
          query.addBindValue(1001);
          query.addBindValue("Thad Beaumont");
          query.addBindValue(65000);
          query.exec();
      

      So prepare(), addBindValue(), exec().

      F 1 Reply Last reply 5 Feb 2018, 14:56
      2
      • J JonB
        5 Feb 2018, 14:11

        @filipdns
        See http://doc.qt.io/qt-5/sql-sqlstatements.html, e.g.

            QSqlQuery query;
            query.prepare("INSERT INTO employee (id, name, salary) "
                          "VALUES (?, ?, ?)");
            query.addBindValue(1001);
            query.addBindValue("Thad Beaumont");
            query.addBindValue(65000);
            query.exec();
        

        So prepare(), addBindValue(), exec().

        F Offline
        F Offline
        filipdns
        wrote on 5 Feb 2018, 14:56 last edited by
        #3

        @JonB Hi, thanks a lot

        here the working query:

                query.prepare("SELECT * FROM table WHERE columnx =\""+yyyy+"\"");
                query.exec();
        

        thank you for your help!!

        J 1 Reply Last reply 5 Feb 2018, 15:25
        0
        • F filipdns
          5 Feb 2018, 14:56

          @JonB Hi, thanks a lot

          here the working query:

                  query.prepare("SELECT * FROM table WHERE columnx =\""+yyyy+"\"");
                  query.exec();
          

          thank you for your help!!

          J Offline
          J Offline
          JonB
          wrote on 5 Feb 2018, 15:25 last edited by
          #4

          @filipdns
          What you have done is very different. You have not used parameters as you seemed to ask for and as my answer gave you, instead you have chosen to expand the variable into the literal text of the SQL query.

          Apart from the fact that this may be (slightly) "inefficient", and that the way you have written it may as well not bother with prepare() but just do the exec() instead, your code will go wrong if the variable has any "unusual" characters in it, e.g. try it when yyyy variable itself includes a " (double-quote) character (whereas your JS tx.executeSql would work correctly).

          I suggest that you heed the code I gave to use addBindValue(), but it's up to you....

          F 1 Reply Last reply 5 Feb 2018, 15:53
          2
          • J JonB
            5 Feb 2018, 15:25

            @filipdns
            What you have done is very different. You have not used parameters as you seemed to ask for and as my answer gave you, instead you have chosen to expand the variable into the literal text of the SQL query.

            Apart from the fact that this may be (slightly) "inefficient", and that the way you have written it may as well not bother with prepare() but just do the exec() instead, your code will go wrong if the variable has any "unusual" characters in it, e.g. try it when yyyy variable itself includes a " (double-quote) character (whereas your JS tx.executeSql would work correctly).

            I suggest that you heed the code I gave to use addBindValue(), but it's up to you....

            F Offline
            F Offline
            filipdns
            wrote on 5 Feb 2018, 15:53 last edited by
            #5

            @JonB Hello, you are right, I used your solution, It will be much better.

                    QSqlQuery query;
                    query.prepare("SELECT * FROM table WHERE columnx =?");
                    query.addBindValue(yyyy);
                    query.exec();
            

            thank you very much!!!

            1 Reply Last reply
            1

            1/5

            5 Feb 2018, 13:55

            • Login

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