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編譯就夠我折騰了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章