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 last edited by Corpse0327
    #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.

    p3c0P 1 Reply Last reply
    0
    • C Corpse0327

      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.

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on 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
      0
      • p3c0P p3c0

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

        p3c0P 1 Reply Last reply
        0
        • C Corpse0327

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

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on 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
          0
          • p3c0P p3c0

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

            p3c0P 1 Reply Last reply
            0
            • C Corpse0327

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

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on 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
              0
              • p3c0P p3c0

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

                • Login

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