• 5731阅读
  • 4回复

Qt_4.3.1使用sqlite问题 [复制链接]

上一主题 下一主题
离线foxyz
 

只看楼主 倒序阅读 楼主  发表于: 2008-03-17
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
最近在作一个新项目,用到sqlite作为一个lightweight的中间数据交换,
发现在我的环境下无法得到一个数据集的记录数,
OS:solaris10
Qt:4.3.1_opensource_x11
sqlite:3.3
代码大概如下:
QSqlDatabase db =QSqlDatabase::addDatabase("QSQLITE", "MY_MID_DB");
db.setDatabase("./Mid.db");

QSqlQuery  query(db);
query.exec("select * from table1");
query.size(); //return -1,truely there have 1 row in table,should return 1

察看了sqlite的技术文档,未发现解决方法。目前的解决方法就是
ret = query.first();
while(ret == true)
{
    ...
....
    ret = query.next();
}

离线zncggaofei
只看该作者 1楼 发表于: 2008-03-17
bool QSqlQuery::exec ( const QString & query )

Executes the SQL in query. Returns true and sets the query state to active if the query was successful; otherwise returns false. The query string must use syntax appropriate for the SQL database being queried (for example, standard SQL).

After the query is executed, the query is positioned on an invalid record and must be navigated to a valid record before data values can be retrieved (for example, using next()).
Note that the last error for this query is reset when exec() is called.

嗯,看文档exec()只返回一个bool值, 在取数据之前必须先定位到有效的记录上
There is someone that is coming or passing away in your life around the clock, so you may lose sight of those seen, and forget those remembered. There is gain and loss in your life, so you may catch sight of those unseen, and remember those forgotten. Nevertheless, doesn’t the unseen exist for sure? Will the remembered remain forever?
离线chencongsytu

只看该作者 2楼 发表于: 2008-03-18
sqlite不支持返回query的大小的
我也很郁闷
如果使用QSqlTableModel的话,也可以如此:
while(model->canFetchMore())
    model->fetchMore();

这还不是最郁闷的,Qt好像有一个缓冲机制,就是一次性只取一部分
比如一个Model本来有500行,它只取256行
需要的时候(比如滚动条拖到最下面)才继续
这时候会带来很多问题:(
离线foxyz

只看该作者 3楼 发表于: 2008-03-18
嘿嘿,有个解决方案是:
int size = 0;
bool rslt=query.exec("select count(*) from mytable");
if(rslt)
{
    rslt = query.first();
    if(rslt)
    {
        size = query.record().field(0).value().toInt();
    }
}
离线zncggaofei
只看该作者 4楼 发表于: 2008-03-18
学习楼上!!!
QT的这种缓冲机制应该是必要的,要不若不禁止不管表有多少数据都取出来,就郁闷了,
要是想全取就
while(model->canFetchMore())
    model->fetchMore();
There is someone that is coming or passing away in your life around the clock, so you may lose sight of those seen, and forget those remembered. There is gain and loss in your life, so you may catch sight of those unseen, and remember those forgotten. Nevertheless, doesn’t the unseen exist for sure? Will the remembered remain forever?
快速回复
限100 字节
 
上一个 下一个