QT SQLite3分页删除问题

前端之家收集整理的这篇文章主要介绍了QT SQLite3分页删除问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

terminal实验

场景:我打算在QT sqlite3中使用分页删除操作。即delete 搭配limit。

如下的例子使用limit、offet sql关键字来实现分页查询
在terminal中实验:

sqlite> select * from test;
1|10
2|20
3|30
sqlite> select * from test order by var2 desc;
3|30
2|20
1|10
sqlite> select * from test order by var2 desc limit 2 offset 2;
1|10
sqlite> select * from test order by var2 desc limit 2 offset 0;
3|30
2|20

删除第一条记录:

sqlite> delete from test limit 1;
sqlite> select * from test;
2|20
3|30

现在,建立数据表test2,使用BIGINT 来存储time(NULL)计算出的从1970年0时0分0秒到现在的秒数。
再使用带order by的条件限制删除操作,

sqlite> select * from test2;
hello|123124
abcse|223124
abc|323124
sqlite> delete from test2 order by num asc limit 1;
sqlite> select * from test2;
abcse|223124
abc|323124

以上均是正常的,没有出现问题。

QT下sqlite分页删除

建立表格:

CREATE TABLE IF NOT EXISTS Vocabulary (myWord text,translation text,secs INTEGER,PRIMARY KEY(myWord,secs))

分页删除
sql语句为 DELETE FROM Vocabulary ORDER BY secs ASC LIMIT :limitNumber
然后再使用query bindValue :limitNumber
后执行,出现信息:query exec Failed: Parameter count mismatch
QT将:limitNumber当作数据表的字段了。
然后我修改成:DELETE FROM Vocabulary ORDER BY secs ASC LIMIT 1
却得到:query exec Failed: No query Unable to fetch row
一般,这样的信息是在说,我的sql语句有问题,但我用终端的sqlite3尝试了,SQL查询是没有语法错误的,神马情况?
接着,我在sqlite官网上查询https://www.sqlite.org 得到如下信息:

Optional LIMIT and ORDER BY clauses:

If sqlite is compiled with the sqlITE_ENABLE_UPDATE_DELETE_LIMIT compile-time option,then the Syntax of the DELETE statement is extended by the addition of optional ORDER BY and LIMIT clause sqlITE_ENABLE_UPDATE_DELETE_LIMIT

This option enables an optional ORDER BY and LIMIT clause on UPDATE and DELETE statements.

If this option is defined,then it must also be defined when using the Lemon parser generator tool to generate a parse.c file. Because of this,this option may only be used when the library is built from source,not from the amalgamation or from the collection of pre-packaged C files provided for non-Unix like platforms on the website.

这解释了为啥terminal中的sqlite3是好用的,但是QT的查询失败了,应该是QT下的sqlite3没有使用sqlITE_ENABLE_UPDATE_DELETE_LIMIT编译,我尝试了Qt的sqlite3 select搭配order by、limit,select是没有问题的。 考虑到跨平台,我还是用文件读写吧,否则sqlite3编译就够我折腾了。

猜你在找的Sqlite相关文章