简体   繁体   中英

Inconsistent result with sqlite delete command with limit enabled

Here are my sqlite db details,

sqlite> .table
url_db
sqlite> .schema url_db
CREATE TABLE url_db(URL TEXT UNIQUE);
sqlite> select * from url_db;
play.googleapis.com
notifications.google.com
contacts.skype.com
edge.skype.com
people.skype.com

I wanted to execute the command delete from url_db limit 1 .

So I've downloaded the full source code (sqlite-src-3240000.zip) from the official download page .

Compiled the source code with the option 'SQLITE_ENABLE_UPDATE_DELETE_LIMIT=1'

When I execute that command (command executed), sometimes it deletes the random entry but not the first entry. I wanted to delete play.googleapis.com , instead that command deleted contacts.skype.com .

sqlite> select * from url_db;
play.googleapis.com
notifications.google.com
edge.skype.com
people.skype.com

What's the cause for this behavior? I am implementing a FIFO list in which when the entries reach 500, I need to delete the first entry.

sometimes it deletes the random entry but not the first entry.

You misunderstand relational databases. A table represents an unordered set. There is no "first" row in a table, unless you explicitly define the ordering.

Because this problem occurs so often, SQLite has a built-in work-around, the rowid "column". You can use this as a regular column, resulting in:

delete from url_db
    order by rowid
    limit 1;

Personally, I prefer an explicitly declared auto-increment column, but SQLite builds in this functions (unlike other databases).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM