Search code examples
qtsqliterecordqsqlquery

How to get the n-th record from QSqlQuery in qt


i try to get data from sqlite, I need to append the n-th and (n+1)-th row from the query to mylist (each item on the list contains n-th and (n+1)-th row) here is my code so far

 QSqlQuery query("SELECT country FROM artist");
 while(query.next()){
   m_datalist.append(new DataObject(query.value("country"),this_field _should_be_the_next_row_with_value contry));
 }

How do i get the n-th and (n+1)-th row from the query at the same time?


Solution

  • Just keep the previous value in a variable:

    QString previous = "";  // whatever the first one should be
    while (query.next()) {
        QString nth = query.value(...);
        ...append(nth, previous);
        previous = nth;
    }