[RESOLVED] QSqlQuery point to value



  • Hallo, how can I get value from multiple rows selected ?

    If want get value from query I do this:
    @QSqlQuery Query;
    Query.exec("SELECT * FROM Table");
    while (Query.next)
    {
    QString Value = Query.value(0).toString;
    }@

    But what if I my query selects more then one row, how can I select value in second or third row.. ?

    I want to do something like this:

    @Query.exec("SELECT Count(*) FROM Table");
    while (Query.next)
    {
    int Count = Query.value(0).toInteger;
    }

    for (int i = 1; i < =Count ; i++)
    {
    QSqlQuery q;
    q.exec(SELECT * FROM Table);

    QString MyString = q.value(0 Column AND int i Row) // Select value from first column and row = i

    // Do Something with MyString
    }@

    Please help, Thank you.
    Sory for my bad English.



  • Hi, if I understand your problem you will receive a set of rows by query.exec.
    You may then go through the rows with query.next and extract all columns.



  • I Can't get it if I my query select from database select more then one row i can't get my values.

    For example:

    My SQL query selection:

    ID value
    1 x
    2 y

    Now if I use this methot:

    @QSqlQuery Query;
    Query.exec("SELECT * FROM Table");
    while (Query.next)
    {
    QString Value = Query.value(0).toString;
    }@

    I get nothing, because it doesin't know wich row value i want asign to QString.

    How can I do that ?

    Is there a way to use query with tvo parameters ROW and COL

    I want to do something like this:

    Query.valye(0,1);

    Sory for my english



  • try this:
    @
    query.prepare("SELECT * FROM table;");
    if (!query.exec()) { // make sure your query has been executed successfully
    qDebug() << query.lastError(); // show the error
    } else {
    while (query.next()) {
    // show the colums of each row:
    qDebug() << query.value(0).toString()
    << query.value(1).toString()
    << query.value(3).toString();
    // or: qDebug() << query.value("value").toString()
    // << query.value("x").toString()
    // << query.value("y").toString();

    }
    }
    @



  • Thank you. It was my mistake :

    @QSqlQuery Query;
    Query.exec("SELECT * FROM Table");
    while (Query.next)
    {
    QString Value = Query.value(0).toString;
    Value = "Some text"
    Do something...
    }@

    I don't know why it didnt worked, at first time, but now it's working.

    Anyways thanks for help.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.