如何从qt中的QSqlQuery中获取第n条记录
问题描述:
我尝试从sqlite中获取数据,我需要将查询中的第n行和第(n + 1)行附加到mylist(列表中包含的第n和这里(N + 1)行)是我到目前为止的代码如何从qt中的QSqlQuery中获取第n条记录
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)个从查询在行同时?
答
只要保持前值的变量:
QString previous = ""; // whatever the first one should be
while (query.next()) {
QString nth = query.value(...);
...append(nth, previous);
previous = nth;
}
答
添加计数器变量:
int i=0;
while(query.next()){
if(i==n || i==n+1)
{
m_datalist.append(...);
}
i++;
}
,或者你可以只选择你需要的记录:
QSqlQuery query("SELECT country FROM artist LIMIT 2 OFFSET "+QString::number(n));
谢谢它的作品像魅力 –