Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

multi row select from SQLite



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



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



  • @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};
    


  • @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();
    }
    


  • @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!!!!!!!!");
        }
    


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



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


Log in to reply