How to delete first few records from a table using SQLite query? - sqlite

I want to delete first 10 records(i.e. records 1 to 10) from my table in SQLite if the record count is 110. Only 100 records I want in my table. If record count is more than 100 then those records should be deleted, as a result if I add new records then also only 100 records should be present. How could it be possible using SQLite query, please provide your suggestions. Thanks.

I gather you want to maintain 100 rows in your table by removing the first 'X' rows every time your table grows beyond 100 rows. Here is what you can do:
Delete from table_name where rowid IN (Select rowid from table_name limit X);
This will keep removing the first 'X' rows as and when your SQLite table grows.

First you have to do a select that returns the last 100 records in your table, then use the delete command to remove all ids not in the result, something like this:
DELETE FROM tbl_name WHERE NOT EXISTS in (SELECT id FROM tbl_name ORDER BY id LIMIT 100 );
Hope this helps!

DELETE FROM {{ table }} LIMIT 0, 10
I'm not entirely sure if that's correct and I don't have the ability to check at this moment.

Related

Is the value specified in OFFSET the same as rowid (sqlite)?

I need a simple scrolling function for my sqlite table, returning the previous X records or the next X records from the last time I selected. As the rule #2 "Do not try to implement a scrolling window using LIMIT and OFFSET" said here, I change to sql from
SELECT * FROM tracks WHERE xxx LIMIT 5 OFFSET :index
to
SELECT * FROM tracks WHERE xxx and rowid > :index LIMIT 5
I do some simple test and the result seems to the same, eg. select rowid from tracks where rowid > 100 limit 5 and select rowid from tracks limit 5 offset 100 both return the same 5 records after rowid 100.
So my question is <1> is the value specified in OFFSET the same as rowid (assume I did not delete record)? <2> Did my change better ?
Update, I know when delete record, rowid may change. So my question is assuming I did not delete record, is the value used in OFFSET the same as rowid ? If not, what exactly does that value mean? And if I do delete some record, what exactly does that value means ?
Also, the article there said "This information is obsolete" but I just can't find the updated information!
When you have deleted some rows, the rowid number are not longer the same as the index.
To show the rows for the next page, you should not use an index value, but the last value of the column that you're sorting by from the last page.

How to read the last record in SQLite table?

Is there a way to read the value of the last record inserted in an SQLite table without going through the previous records ?
I ask this question for performance reasons.
There is a function named sqlite3_last_insert_rowid() which will return the integer key for the most recent insert operation. http://www.sqlite.org/c3ref/last_insert_rowid.html
This only helps if you know the last insert happened on the table you care about.
If you need the last row on a table, regardless of wehter the last insert was on this table or not, you will have to use a SQL query
SELECT * FROM mytable WHERE ROWID IN ( SELECT max( ROWID ) FROM mytable );
When you sort the records by ID, in reverse order, the last record will be returned first.
(Because of the implicit index on the autoincrementing column, this is efficient.)
If you aren't interested in any other records, use LIMIT:
SELECT *
FROM MyTable
ORDER BY _id DESC
LIMIT 1

How to delete the next n rows in sqlite

How could I delete all the rows after the 20th in sqlite, in another way I need to maintain a database with only 20 rows at each new insert I need to delete the oldest row inserted
DELETE FROM stuff
WHERE rowid NOT IN (
SELECT rowid FROM stuff ORDER BY created DESC LIMIT 20);
See http://sqlfiddle.com/#!7/a3e2f/9

Efficient paging in SQLite with millions of records

I need to show the SQLite results in a list view. Of course, I need to page the results.
The first option is to use the LIMIT clause. For example:
SELECT * FROM Table LIMIT 100, 5000
It returns records 5001 to 5100. The problem is that internally SQLite "reads" the first 5000 records and it is not too efficient.
What is the best approach for paging when there are a lot of records?
Please note that you always have to use an ORDER BY clause; otherwise, the order is arbitrary.
To do efficient paging, save the first/last displayed values of the ordered field(s), and continue just after them when displaying the next page:
SELECT *
FROM MyTable
WHERE SomeColumn > LastValue
ORDER BY SomeColumn
LIMIT 100;
(This is explained with more detail on the SQLite wiki.)
When you have multiple sort columns (and SQLite 3.15 or later), you can use a row value comparison for this:
SELECT *
FROM MyTable
WHERE (SomeColumn, OtherColumn) > (LastSome, LastOther)
ORDER BY SomeColumn, OtherColumn
LIMIT 100;

sqlite delete row count

I need to write a SQLlite query that will delete rows from a table above 200. I was thinking this would work:
DELETE FROM [tbl_names] WHERE count(*) > 200
but that gives me: misuse of aggregate function count()
I know there is a limit clause I can use, but if I use:
DELETE FROM [tbl_names] LIMIT 200
that looks like it will delete the first 200 rows.
All rows in an SQLite have rowid field, which you can use to find rows greater than 200. For example:
DELETE FROM [tbl_names] WHERE rowid > 200
You could also use an offset with your limit:
DELETE FROM [tbl_names] LIMIT 10000 offset 200
using the roqid seems to be the better choice.

Resources