Is it possible in SQLite to limit the size of a table to the number of records and how to organize it? For example, the table lists a total of 100 rows, after which the oldest entries are deleted and new ones are written.
3 answers
You can write a trigger to insert new records into the table.
Suppose you have a table _table with fields
_id(key of some kind)_time(record creation time)
Then the trigger will look like this.
CREATE TRIGGER delete_till_50 INSERT ON _table WHEN (select count(*) from _table)>100 BEGIN DELETE FROM _table WHERE _table._id IN (SELECT _table._id FROM _table ORDER BY _table._time limit (select count(*) - 100 from _table )); END; When you insert a new entry, the oldest entries will be deleted. The database will always be no more than 100 entries.
|
No, it is impossible to limit the number of entries, but there is one trick that can help:
- Create table
- Fill it with hundreds of records, each one millisecond older than the previous one.
- Instead of an insert operation, perform an update operation on the oldest entry.
|
