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. multi row select from SQLite
Forum Updated to NodeBB v4.3 + New Features

multi row select from SQLite

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 864 Views 1 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.
  • S Offline
    S Offline
    SeyMohsenFls
    wrote on last edited by
    #1

    Hi friends, i want to select at least two rows from SQL, but i can't, can you help me guys??

    artwawA 1 Reply Last reply
    0
    • S SeyMohsenFls

      Hi friends, i want to select at least two rows from SQL, but i can't, can you help me guys??

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      @SeyMohsenFls Please show your code. How are you trying to do this?

      For more information please re-read.

      Kind Regards,
      Artur

      S 1 Reply Last reply
      3
      • artwawA artwaw

        @SeyMohsenFls Please show your code. How are you trying to do this?

        S Offline
        S Offline
        SeyMohsenFls
        wrote on last edited by
        #3

        @artwaw i can just get one number like this:

        QSqlQuery q1;
            QString comp1= ui->lineEdit->text();
            QString comp2= ui->lineEdit_2->text();
        q1.exec("SELECT ["+comp2+"] From Par_A WHERE C_id='"+comp1+"'");
        

        but i want get a row of data from a table in SQL then move that to a matrix like this:

        a[5]={0,5,9,7,3};
        
        artwawA 1 Reply Last reply
        0
        • S SeyMohsenFls

          @artwaw i can just get one number like this:

          QSqlQuery q1;
              QString comp1= ui->lineEdit->text();
              QString comp2= ui->lineEdit_2->text();
          q1.exec("SELECT ["+comp2+"] From Par_A WHERE C_id='"+comp1+"'");
          

          but i want get a row of data from a table in SQL then move that to a matrix like this:

          a[5]={0,5,9,7,3};
          
          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          @SeyMohsenFls I think the pair of [] is not necessary, you can just simplify query to "select "+comp2+"from Par_A where C_id="+comp1+";" - sqlite is quite relaxed. if course, if C_id is of type text ' use might be necessary.
          When you run your original query - what does q1.lastQuery() return? What is the return value of q1.lastError().text()?

          You can add qDebug() << q1.lastQuery(); qDebug() << q1.lastError().text(); after the exec, this should throw out results into the console.

          As for processing results, QSqlQuery documentation webpage tells you how to proceed (I assume you want a list of int):

          QVector<int> results;
          while (q1.next()) {
             results.append(q1.value(0).toInt();
          }
          

          For more information please re-read.

          Kind Regards,
          Artur

          S 1 Reply Last reply
          0
          • artwawA artwaw

            @SeyMohsenFls I think the pair of [] is not necessary, you can just simplify query to "select "+comp2+"from Par_A where C_id="+comp1+";" - sqlite is quite relaxed. if course, if C_id is of type text ' use might be necessary.
            When you run your original query - what does q1.lastQuery() return? What is the return value of q1.lastError().text()?

            You can add qDebug() << q1.lastQuery(); qDebug() << q1.lastError().text(); after the exec, this should throw out results into the console.

            As for processing results, QSqlQuery documentation webpage tells you how to proceed (I assume you want a list of int):

            QVector<int> results;
            while (q1.next()) {
               results.append(q1.value(0).toInt();
            }
            
            S Offline
            S Offline
            SeyMohsenFls
            wrote on last edited by
            #5

            @artwaw
            i got this from that qDebug:

            "SELECT [4] From Par_A WHERE C_id='2'"
            ""
            

            there is no error.
            i have another question. how can i input the value of a query to a variable before check the query?
            i wrote this code but it's wrong!

            QSqlQuery q1;
            q1.exec("SELECT ["+comp2+"] From Par_A WHERE C_id='"+comp1+"'");
            double a=q1.value(0).toDouble();
            
            if(q1.first() )
                {
                 //i dont want write ****a=q1.value(0).toDouble();**** here!!!!!
                }
                else {
                    ui->text1->setText("not found!!!!!!!!");
                }
            
            artwawA 1 Reply Last reply
            0
            • S SeyMohsenFls

              @artwaw
              i got this from that qDebug:

              "SELECT [4] From Par_A WHERE C_id='2'"
              ""
              

              there is no error.
              i have another question. how can i input the value of a query to a variable before check the query?
              i wrote this code but it's wrong!

              QSqlQuery q1;
              q1.exec("SELECT ["+comp2+"] From Par_A WHERE C_id='"+comp1+"'");
              double a=q1.value(0).toDouble();
              
              if(q1.first() )
                  {
                   //i dont want write ****a=q1.value(0).toDouble();**** here!!!!!
                  }
                  else {
                      ui->text1->setText("not found!!!!!!!!");
                  }
              
              artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #6

              @SeyMohsenFls pls try to apply the query I wrote (leave the check code just in case I did something wrong). Do not forget the ; at the end.

              You can't, while (query.next()) is the way to go as far as I can tell. Please read the documentation.

              For more information please re-read.

              Kind Regards,
              Artur

              S 1 Reply Last reply
              1
              • artwawA artwaw

                @SeyMohsenFls pls try to apply the query I wrote (leave the check code just in case I did something wrong). Do not forget the ; at the end.

                You can't, while (query.next()) is the way to go as far as I can tell. Please read the documentation.

                S Offline
                S Offline
                SeyMohsenFls
                wrote on last edited by
                #7

                @artwaw
                thank you. I read documentation and problem solved.

                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