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 SELECT
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery SELECT

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 2.6k 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.
  • G Offline
    G Offline
    gabor53
    wrote on 19 Sept 2016, 03:46 last edited by
    #1

    Hi,
    I use QSQLITE. I would like to choose a record from a database based in the ID number. I have the following:

      QSqlQuery query_getImage ("SELECT Pic FROM Items WHERE ID == FriendID");
    

    FriendID is a QString. How should I write this line correctly so it recognizes FriendID as a QString?
    Thank you.

    J M 2 Replies Last reply 19 Sept 2016, 04:18
    0
    • G gabor53
      19 Sept 2016, 03:46

      Hi,
      I use QSQLITE. I would like to choose a record from a database based in the ID number. I have the following:

        QSqlQuery query_getImage ("SELECT Pic FROM Items WHERE ID == FriendID");
      

      FriendID is a QString. How should I write this line correctly so it recognizes FriendID as a QString?
      Thank you.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 19 Sept 2016, 04:18 last edited by
      #2

      @gabor53 Do you mean FriendID is a QString variable?
      You should take a look at the documentation. It even contains examples which answer your question: http://doc.qt.io/qt-5/qsqlquery.html

      QSqlQuery query;
      query.prepare("INSERT INTO person (id, forename, surname) "
                        "VALUES (:id, :forename, :surname)");
      query.bindValue(":id", 1001);
      query.bindValue(":forename", "Bart");
      query.bindValue(":surname", "Simpson");
      query.exec();
      

      In this example id, forename and surename are replaced by int and string (you can use your variable instead of a string literal).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • V Offline
        V Offline
        VRonin
        wrote on 19 Sept 2016, 06:54 last edited by
        #3

        SQL does not use ==

         QSqlQuery query_getImage;
        query_getImage.prepare("SELECT Pic FROM Items WHERE ID = :friendID");
        query_getImage.bindValue(":friendID",FriendID);
        if(query_getImage.exec()){
        // success
        }
        

        if FriendID is a string but you want it to go into the query as an int use query_getImage.bindValue(":friendID",FriendID.toInt()); on the other hand if FriendID is int and you want it to go in as a string use query_getImage.bindValue(":friendID",QString::number(FriendID));

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        G 1 Reply Last reply 19 Sept 2016, 14:41
        1
        • V VRonin
          19 Sept 2016, 06:54

          SQL does not use ==

           QSqlQuery query_getImage;
          query_getImage.prepare("SELECT Pic FROM Items WHERE ID = :friendID");
          query_getImage.bindValue(":friendID",FriendID);
          if(query_getImage.exec()){
          // success
          }
          

          if FriendID is a string but you want it to go into the query as an int use query_getImage.bindValue(":friendID",FriendID.toInt()); on the other hand if FriendID is int and you want it to go in as a string use query_getImage.bindValue(":friendID",QString::number(FriendID));

          G Offline
          G Offline
          gabor53
          wrote on 19 Sept 2016, 14:41 last edited by
          #4

          @VRonin
          I did the following:

           QSqlQuery query_getImage.prepare ("SELECT Pic FROM Items WHERE ID = :FriendID");
          query_getImage.bindValue (":ID",FriendID);
                      if(query_getImage.exec())
                          {
                              qDebug() << "The query is active.";
                          }
                      else
                          {
                              qDebug() << "The query is NOT active." << query_getImage.lastError ();
                          }
          

          I get the following error messages:

          C:\Programming\Projects\Folkfriends_1_0\mainwindow.cpp:123: error: expected initializer before '.' token
          QSqlQuery query_getImage.prepare ("SELECT Pic FROM Items WHERE ID = :FriendID");
          ^
          C:\Programming\Projects\Folkfriends_1_0\mainwindow.cpp:124: error: 'query_getImage' was not declared in this scope
          query_getImage.bindValue (":ID",FriendID);
          ^
          What did I miss?

          1 Reply Last reply
          0
          • V Offline
            V Offline
            VRonin
            wrote on 19 Sept 2016, 15:02 last edited by VRonin
            #5

            you have to separate the declaration from the use

             QSqlQuery query_getImage;
             query_getImage.prepare ("SELECT Pic FROM Items WHERE ID = :FriendID");
            

            also query_getImage.bindValue (":ID",FriendID); is wrong, it should be query_getImage.bindValue (":FriendID",FriendID);

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1
            • G gabor53
              19 Sept 2016, 03:46

              Hi,
              I use QSQLITE. I would like to choose a record from a database based in the ID number. I have the following:

                QSqlQuery query_getImage ("SELECT Pic FROM Items WHERE ID == FriendID");
              

              FriendID is a QString. How should I write this line correctly so it recognizes FriendID as a QString?
              Thank you.

              M Offline
              M Offline
              mjsurette
              wrote on 19 Sept 2016, 18:02 last edited by mjsurette
              #6

              @gabor53
              Another idiom I use if I'm only running a query once is

              QSqlQuery query_getImage();
              QString qryTxt= "SELECT Pic FROM Items WHERE ID == '%1'; ";
              query_getImage.exec(qryTxt.arg(FriendID));
              

              Mike

              V 1 Reply Last reply 19 Sept 2016, 18:09
              -1
              • M mjsurette
                19 Sept 2016, 18:02

                @gabor53
                Another idiom I use if I'm only running a query once is

                QSqlQuery query_getImage();
                QString qryTxt= "SELECT Pic FROM Items WHERE ID == '%1'; ";
                query_getImage.exec(qryTxt.arg(FriendID));
                

                Mike

                V Offline
                V Offline
                VRonin
                wrote on 19 Sept 2016, 18:09 last edited by VRonin
                #7

                @mjsurette please don't! especially if FriendID can be somehow manipulated by the user. See here for the reason why. I'll just quote the first line:

                [It] can destroy your database

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                M 1 Reply Last reply 19 Sept 2016, 23:17
                1
                • V VRonin
                  19 Sept 2016, 18:09

                  @mjsurette please don't! especially if FriendID can be somehow manipulated by the user. See here for the reason why. I'll just quote the first line:

                  [It] can destroy your database

                  M Offline
                  M Offline
                  mjsurette
                  wrote on 19 Sept 2016, 23:17 last edited by
                  #8

                  @VRonin

                  Thanks for the heads up.

                  Mike.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    gabor53
                    wrote on 20 Sept 2016, 03:42 last edited by
                    #9

                    The following worked for me:

                    QSqlQuery query_getImage;
                                query_getImage.prepare ("SELECT Pic FROM Items WHERE ID = :FriendID");
                                query_getImage.bindValue (":FriendID",FriendID);
                    
                                if(query_getImage.exec())
                                    {
                                        qDebug() << "The query query_getImage is active.";
                                    }
                                else
                                    {
                                        qDebug() << "The query is NOT active." << query_getImage.lastError ();
                                    }
                    
                                query_getImage.first ();
                                QByteArray pixArray;
                                pixArray = query_getImage.value(0).toByteArray ();
                    
                                qDebug() << "pixArray size: " << pixArray.size ();
                                QPixmap Pixmap;
                                Pixmap.loadFromData (pixArray);
                    

                    Thank you for all your help.

                    1 Reply Last reply
                    0

                    1/9

                    19 Sept 2016, 03:46

                    • Login

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