2017-05-05 70 views
0

我嘗試從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)個從查詢在行同時?

回答

0

只要保持前值的變量:

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

謝謝它的作品像魅力 –

0

添加計數器變量:

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)); 
相關問題