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() Function fails to Bind
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery bindValue() Function fails to Bind

Scheduled Pinned Locked Moved General and Desktop
qsqlquery
7 Posts 2 Posters 4.7k 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.
  • C Offline
    C Offline
    Corpse0327
    wrote on 11 Aug 2015, 18:02 last edited by Corpse0327 8 Nov 2015, 18:03
    #1

    Hi

    I am having a trouble with QSqlQuery class' bindValue()
    The code:

    query.prepare("SELECT NAME FROM SQLITE_MASTER WHERE TYPE='table' AND NAME=:table");
    query.bindValue(":table", table);
    
    if(query.exec() == false)
    {
        qDebug() << "DataIO::createTableIf:1: " << query.lastError().text();
    }
    qDebug() << query.executedQuery();
    

    The qDebug() prints this

    "SELECT NAME FROM SQLITE_MASTER WHERE TYPE='table' AND NAME=?"
    

    I can't understand why bindValue() doesn't binds the value and just binds "?".

    Is this something with runtime? I may as well use the old way:

    query.exec("SELECT NAME FROM SQLITE_MASTER WHERE TYPE='table' AND NAME=" + table);
    

    But i want to use the right way of doing things. Thanks in advance for any help.

    P 1 Reply Last reply 12 Aug 2015, 08:58
    0
    • C Corpse0327
      11 Aug 2015, 18:02

      Hi

      I am having a trouble with QSqlQuery class' bindValue()
      The code:

      query.prepare("SELECT NAME FROM SQLITE_MASTER WHERE TYPE='table' AND NAME=:table");
      query.bindValue(":table", table);
      
      if(query.exec() == false)
      {
          qDebug() << "DataIO::createTableIf:1: " << query.lastError().text();
      }
      qDebug() << query.executedQuery();
      

      The qDebug() prints this

      "SELECT NAME FROM SQLITE_MASTER WHERE TYPE='table' AND NAME=?"
      

      I can't understand why bindValue() doesn't binds the value and just binds "?".

      Is this something with runtime? I may as well use the old way:

      query.exec("SELECT NAME FROM SQLITE_MASTER WHERE TYPE='table' AND NAME=" + table);
      

      But i want to use the right way of doing things. Thanks in advance for any help.

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 12 Aug 2015, 08:58 last edited by
      #2

      @Corpse0327 Did you check if you get the output ?
      As per the doc it will return result same as lastQuery() but in case if the db does not support placeholders then a new query is formed with bound values and I guess in that case executedQuery() will show the values. More info here.

      157

      C 1 Reply Last reply 12 Aug 2015, 11:28
      0
      • P p3c0
        12 Aug 2015, 08:58

        @Corpse0327 Did you check if you get the output ?
        As per the doc it will return result same as lastQuery() but in case if the db does not support placeholders then a new query is formed with bound values and I guess in that case executedQuery() will show the values. More info here.

        C Offline
        C Offline
        Corpse0327
        wrote on 12 Aug 2015, 11:28 last edited by
        #3

        @p3c0 said:

        @Corpse0327 Did you check if you get the output ?
        As per the doc it will return result same as lastQuery() but in case if the db does not support placeholders then a new query is formed with bound values and I guess in that case executedQuery() will show the values. More info here.

        Well, the function returns the latest succesfully executed query. It returns the query i added only with placeholders replaced with quos. So it means it executed query with quos.

        I checked it from sqldb using SqliteMan. By the way it is not just limited to this query. At any query i prepared placeholders are replaced by quos.

        P 1 Reply Last reply 12 Aug 2015, 11:31
        0
        • C Corpse0327
          12 Aug 2015, 11:28

          @p3c0 said:

          @Corpse0327 Did you check if you get the output ?
          As per the doc it will return result same as lastQuery() but in case if the db does not support placeholders then a new query is formed with bound values and I guess in that case executedQuery() will show the values. More info here.

          Well, the function returns the latest succesfully executed query. It returns the query i added only with placeholders replaced with quos. So it means it executed query with quos.

          I checked it from sqldb using SqliteMan. By the way it is not just limited to this query. At any query i prepared placeholders are replaced by quos.

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 12 Aug 2015, 11:31 last edited by
          #4

          @Corpse0327 Since sqlite db supports placeholders new query with those bound values will not be formed and it will show question mark as I understand from the doc.

          157

          C 1 Reply Last reply 12 Aug 2015, 14:54
          0
          • P p3c0
            12 Aug 2015, 11:31

            @Corpse0327 Since sqlite db supports placeholders new query with those bound values will not be formed and it will show question mark as I understand from the doc.

            C Offline
            C Offline
            Corpse0327
            wrote on 12 Aug 2015, 14:54 last edited by
            #5

            @p3c0 Forgive me if i misunderstood. You are saying that it would show question marks but would work properly right? Since Sqlite supports placeholders.

            I tested it many times and, no. It just executes the query with question marks. I have changed my code to 'without bindValue()' version. Everything works fine.

            P 1 Reply Last reply 13 Aug 2015, 05:50
            0
            • C Corpse0327
              12 Aug 2015, 14:54

              @p3c0 Forgive me if i misunderstood. You are saying that it would show question marks but would work properly right? Since Sqlite supports placeholders.

              I tested it many times and, no. It just executes the query with question marks. I have changed my code to 'without bindValue()' version. Everything works fine.

              P Offline
              P Offline
              p3c0
              Moderators
              wrote on 13 Aug 2015, 05:50 last edited by p3c0
              #6

              @Corpse0327

              Forgive me if i misunderstood. You are saying that it would show question marks but would work properly right? Since Sqlite supports placeholders.

              Yes.

              I tested it many times and, no. It just executes the query with question marks. I have changed my code to 'without bindValue()' version. Everything works fine.

              Are you sure ? I hope you are checking in the following manner after query execution

              while(query.next())
                      qDebug() << query.value(0).toString(); //get value at 0 column
              

              Also does query.exec() returns false in this case ?

              157

              C 1 Reply Last reply 13 Aug 2015, 11:32
              0
              • P p3c0
                13 Aug 2015, 05:50

                @Corpse0327

                Forgive me if i misunderstood. You are saying that it would show question marks but would work properly right? Since Sqlite supports placeholders.

                Yes.

                I tested it many times and, no. It just executes the query with question marks. I have changed my code to 'without bindValue()' version. Everything works fine.

                Are you sure ? I hope you are checking in the following manner after query execution

                while(query.next())
                        qDebug() << query.value(0).toString(); //get value at 0 column
                

                Also does query.exec() returns false in this case ?

                C Offline
                C Offline
                Corpse0327
                wrote on 13 Aug 2015, 11:32 last edited by Corpse0327
                #7

                @p3c0 Thank you for your help

                I have realized that

                1. If query execution is a failure, than lastQuery() returns a string with either placeHolders or placeHolders replaced with quos. Making the user thinking something with binding is wrong.
                2. If query is succesful, lastQuery() returns either placeHolders replaced with quos or actual values.

                I am going to further test things out and see if something may be improved with bindValue() and make a request based on that.

                1 Reply Last reply
                0

                1/7

                11 Aug 2015, 18:02

                • 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