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. Why doesn't bind work with this query

Why doesn't bind work with this query

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 3.9k Views 3 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.
  • AsimovA Offline
    AsimovA Offline
    Asimov
    wrote on last edited by
    #1

    OK the following query works

        QSqlQuery query;
        query.prepare("select `boxes`.`boxID`,`boxName`,`boxLocation` from `boxes`"\
                      "LEFT JOIN `boxContents` on `boxes`.`boxID`=`boxContents`.`boxID` WHERE `itemName` LIKE '%be%'");
        query.exec();
    

    I am searching for the word 'be' in the database.

    However I want to do this using a string because I want to make a function and so I did this.

        QString search="be";
        QSqlQuery query;
        query.prepare("select `boxes`.`boxID`,`boxName`,`boxLocation` from `boxes`"\
                      "LEFT JOIN `boxContents` on `boxes`.`boxID`=`boxContents`.`boxID` WHERE `itemName` LIKE '%:search%'");
        query.bindValue(":search",search);
        query.exec();
    

    And I am not getting any results. It is like the bind isn't working.

    What could be wrong?

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

      Hi,

      Please check the return value of exec and print the error if the call fails.

      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
      • AsimovA Offline
        AsimovA Offline
        Asimov
        wrote on last edited by
        #3

        @SGaist

        I did this

        qDebug() << query.lastError();
        

        And the response I got was this

        QSqlError("", "", "")
        

        So no error, but the bind does not seem to be working. I know the query is fine however, as the first bit of code works.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mawh1960
          wrote on last edited by
          #4

          Try comparing the actual query strings that are being sent to the server for your two queries:

          qDebug() << query.lastQuery();
          
          AsimovA 1 Reply Last reply
          0
          • M mawh1960

            Try comparing the actual query strings that are being sent to the server for your two queries:

            qDebug() << query.lastQuery();
            
            AsimovA Offline
            AsimovA Offline
            Asimov
            wrote on last edited by Asimov
            #5

            @mawh1960
            Here are the two strings from query.lastquery();

            "select `boxes`.`boxID`,`boxName`,`boxLocation` from `boxes`LEFT JOIN `boxContents` on `boxes`.`boxID`=`boxContents`.`boxID` WHERE `itemName` LIKE '%:search%'"
            "select `boxes`.`boxID`,`boxName`,`boxLocation` from `boxes`LEFT JOIN `boxContents` on `boxes`.`boxID`=`boxContents`.`boxID` WHERE `itemName` LIKE '%be%'"
            

            The second one works, and that is when I hard code in the letters "be".
            The first one didn't work.

            LOL sometimes php is so much easier heh heh

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              You can use a tool like http://sqlitebrowser.org/
              to verify your select actually works. Meaning it returns any rows.

              1 Reply Last reply
              0
              • AsimovA Offline
                AsimovA Offline
                Asimov
                wrote on last edited by
                #7

                @mrjj

                The select does actually work, I know it works. What is not working is the bind in QT. I already tried it in SQLite manager in firefox.

                Also the select works in QT when I hard code the value. It doesn't work when I use :search variable in the line.
                For some reason the value is not binding, even though I have put the bind line in.

                1 Reply Last reply
                0
                • AsimovA Offline
                  AsimovA Offline
                  Asimov
                  wrote on last edited by Asimov
                  #8

                  I would say I solved the problem, but it was a post I found on stack overflow that solved it really. Here is the solution

                   QString search="be";
                      QSqlQuery query;
                      query.prepare("select `boxes`.`boxID`,`boxName`,`boxLocation` from `boxes`"\
                                    "LEFT JOIN `boxContents` on `boxes`.`boxID`=`boxContents`.`boxID` WHERE `itemName` LIKE '%'||:search||'%'");
                      query.bindValue(":search",search);
                      query.exec();
                  

                  Instead of

                  LIKE '%:search%'
                  

                  I had to put

                  LIKE '%'||:search||'%'
                  

                  I haven't come across this before because I mainly work in websites using php and mysql.
                  However for this project I am using QT and sqlite.

                  This is the stack overflow post that solved my question
                  http://stackoverflow.com/questions/13111130/qt-qsqlquery-prepare-and-bindvalue-not-working

                  1 Reply Last reply
                  2
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Ok
                    super
                    So it was the actual bind syntax that was wrong when using the like operator
                    Thx for reporting back :)

                    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