• 13647阅读
  • 10回复

SQLite不支持QSqlQuery::size()? [复制链接]

上一主题 下一主题
离线yinrenjieone
 

只看楼主 正序阅读 楼主  发表于: 2011-08-12
我的
sourceQuery.exec("select student from publicSource order by question");
qDebug()<<sourceQuery.size();
一直返回-1
离线xiaoxianlg
只看该作者 10楼 发表于: 2012-07-19
如果,你只是想判断query的大小是否为0(而不是为了真正获取其size),可以这么做
if(query.next())
{
    /*做你想做的*/
    while(query.next())
    {
        /*做你想做的*/
    }
}
else
{
    /*做你另外想做的*/
}
我是根据书上这么一句话想到的,因为我当时只想知道size是否为0而不是想获得具体大小。
书上的话:如果结果集为空,(或者查询失败),那么next()的第一次调用将返回false。
里面有提到结果集为空。

如果,想获取真正的size大小。
方法一:
if (query.last())
{
    return query.at() + 1;
}
方法二:
int rowCount = 0;
while (query.next())
{
    rowCount++;
}
return rowCount;
离线xiaoxianlg
只看该作者 9楼 发表于: 2012-07-19
如果,你只是想判断query的大小是否为0(而不是为了真正获取其size),可以这么做
if(query.next())
{
    /*做你想做的*/

}
离线hehui

只看该作者 8楼 发表于: 2011-10-10
不是Qt的问题,手册上说了,有些驱动不返回select的大小!
试试先用fetchMore()取出所有数据
用ODBC取SQL Server的数据时要想知道大小,就要这样干!否则它一次只返回一部分(可能200条)
离线jianxin.jin
只看该作者 7楼 发表于: 2011-08-31
这个函数是有问题,我用的时候总返回0,刚开始我还以为是数据库有问题,后来才发现是他这个函数本身有问题,还是用sql语句查询一下吧
离线yinrenjieone

只看该作者 6楼 发表于: 2011-08-14
Re:回 4楼(yinrenjieone) 的帖子
引用第5楼wxj120bw于2011-08-14 12:18发表的 回 4楼(yinrenjieone) 的帖子 :
你咋不用select count(*) from ***  [表情]  

根本不知道有这样的用法
离线wxj120bw

只看该作者 5楼 发表于: 2011-08-14
回 4楼(yinrenjieone) 的帖子
你咋不用select count(*) from ***  
离线yinrenjieone

只看该作者 4楼 发表于: 2011-08-13
Re:回 2楼(yinrenjieone) 的帖子
引用第3楼wxj120bw于2011-08-13 15:43发表的 回 2楼(yinrenjieone) 的帖子 :
看下qt的assistant帮助对这个函数的说明
Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes. Note that for non-SELECT statements (isSelect() returns false), size() will return -1. If the query is not active (isActive() returns false), -1 is returned.
To determine the number of rows affected by a non-SELECT statement, use numRowsAffected().

呃……那好像是真的不支持啊……
我现在每次都要查两遍,第一遍用
while(query.next())
count++;
来计数……
感觉总是麻烦……
离线wxj120bw

只看该作者 3楼 发表于: 2011-08-13
回 2楼(yinrenjieone) 的帖子
看下qt的assistant帮助对这个函数的说明
Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes. Note that for non-SELECT statements (isSelect() returns false), size() will return -1. If the query is not active (isActive() returns false), -1 is returned.

To determine the number of rows affected by a non-SELECT statement, use numRowsAffected().
离线yinrenjieone

只看该作者 2楼 发表于: 2011-08-13
Re:回 楼主(yinrenjieone) 的帖子
引用第1楼wxj120bw于2011-08-12 22:02发表的 回 楼主(yinrenjieone) 的帖子 :
qsql里有这个函数lastError(),你怎么不用呢?

qDebug()<<soourceQuery.lastError();
输出QSqlError(-1, "", "")
然后呢?
离线wxj120bw

只看该作者 1楼 发表于: 2011-08-12
回 楼主(yinrenjieone) 的帖子
qsql里有这个函数lastError(),你怎么不用呢?
快速回复
限100 字节
 
上一个 下一个