QT SQLite3分页删除问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/theArcticOcean/article/details/79650659

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编译就够我折腾了。

猜你喜欢

转载自blog.csdn.net/theArcticOcean/article/details/79650659