如何在qt中从QSqlQuery获取第n条记录

如何在qt中从QSqlQuery获取第n条记录,qt,sqlite,record,qsqlquery,Qt,Sqlite,Record,Qsqlquery,我试图从sqlite获取数据,我需要将查询中的第n行和第(n+1)行附加到mylist(列表中的每个项目都包含第n行和第(n+1)行),这是我到目前为止的代码 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_v

我试图从sqlite获取数据,我需要将查询中的第n行和第(n+1)行附加到mylist(列表中的每个项目都包含第n行和第(n+1)行),这是我到目前为止的代码

 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));
 }

如何同时从查询中获取第n行和(n+1)行?

添加一个计数器变量:

int i=0;
while(query.next()){
   if(i==n || i==n+1)
    {
       m_datalist.append(...);
    }
    i++;
 }
QString previous = "";  // whatever the first one should be
while (query.next()) {
    QString nth = query.value(...);
    ...append(nth, previous);
    previous = nth;
}
或者,您可以仅选择所需的记录:

QSqlQuery query("SELECT country FROM artist LIMIT 2 OFFSET "+QString::number(n));

只需将上一个值保留在变量中:

int i=0;
while(query.next()){
   if(i==n || i==n+1)
    {
       m_datalist.append(...);
    }
    i++;
 }
QString previous = "";  // whatever the first one should be
while (query.next()) {
    QString nth = query.value(...);
    ...append(nth, previous);
    previous = nth;
}