QSqlQuery size() 总是返回 -1 [英] QSqlQuery size() always returns -1

查看:75
本文介绍了QSqlQuery size() 总是返回 -1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

QSqlQuery query;
QString queryText("SELECT * FROM section");
query.exec(queryText);
qDebug() << query.size(); //always -1
while (query.next()) qDebug() << query.value(0).toString(); //got 16 records

方法 size() 总是返回 -1.请帮忙.谢谢.

Method size() always returns -1. Help, please. Thanks.

推荐答案

query.size() 不支持 SQLite.但是您可以通过解决方法获得行数.QSqlQuery::last () 检索结果中的最后一条记录(如果可用),并将查询定位在检索到的记录上.调用 last() 后,您可以使用 first()previous() 检索最后一条记录的索引并将查询定位在第一条记录之前:

query.size() is not supported with SQLite. But you can get the number of rows with a workaround. QSqlQuery::last () retrieves the last record in the result, if available, and positions the query on the retrieved record. After calling last() you can retrieve index of the last record and position the query before the first record using first() and previous() :

int numberOfRows = 0;
if(qry.last())
{
    numberOfRows =  qry.at() + 1;
    qry.first();
    qry.previous(); 
}

这篇关于QSqlQuery size() 总是返回 -1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆