Delete rows with inner join? - android

I have a SQLITE database with two tables. Table A has an integer timestamp and another integer column containing a row id referring to a row in table B which has two timestamps.
I want to delete all rows in table A where it's timestamp does not lie between the two timestamps in table B, and the ROWID is equal to X.
Here is what I have at the moment but I am getting a syntax error:
DELETE FROM network
WHERE ROWID in (
SELECT ROWID
FROM track
INNER JOIN network ON (track.ROWID = network.trackId)
WHERE network.timestamp > track.stopTime OR network.timestamp < track.startTime
AND network.trackId = X

You don't have a closing parenthesis for your subselect. Try this:
DELETE FROM network
WHERE ROWID in (
SELECT ROWID
FROM track
INNER JOIN network ON (track.ROWID = network.trackId)
WHERE network.timestamp > track.stopTime OR network.timestamp < track.startTime
AND network.trackId = X
)
If that doesn't work, try posting your actual syntax error.

Related

Limiting the Number of Rows Deletes in SQLITE DB

Anyone know of a way to limit the number of rows deleted when using an sql DELETE statement?I just need to delete a row that holds a certain value one time instead of deleting every instance of the value. It's my understanding that the LIMIT clause cannot be added to DELETE statements in SQLITE. Now, I can't see a way to limit the number of rows deleted just using _id because I don't know what row _id will be deleted ahead of time; the rows are being deleted based on a value held in a variable and they could be anywhere in the DB. I hope this makes sense. Here's the delete statement:
String sql = "DELETE FROM strategyTotal WHERE strategy_prices = (?)" ;
db.execSQL(sql, new Double[] {subtractedStrategyPrice });
Use a subquery:
String sql = "DELETE FROM strategyTotal WHERE _id IN (SELECT _id FROM strategyTotal WHERE strategy_prices = (?) LIMIT 1);" ;
db.execSQL(sql, new Double[] {subtractedStrategyPrice });
delete from tablename where rowid in (
select rowid from tablename condition LIMIT 1)
try above work around or you may need to enable SQLITE ENABLE UPDATE DELETE LIMIT
my query is just an example. replace it with your own query.

complex trigger on SQLite

It's possible create on SQLite a "complex" trigger? for complex i mean that trigger body should provide to count row inside a table, then if count it's greater than a fixed value, delete some rows for satisfy previuos condition
You should be able to do that using the WHERE clause of the trigger definition, eg WHERE count(*>100. Then in the action part of the trigger, define a DELETE statement with a WHERE clause that identifies which "oldest" entry you want to delete.
Assuming that ID is an autoincrementing column, the following query would find those records with the 100 highest ID values, i.e., those that should not be deleted:
SELECT *
FROM MyTable
ORDER BY ID DESC
LIMIT 100
This allows to write the following trigger:
CREATE TRIGGER DeleteOldestMoreThan100
AFTER INSERT ON MyTable
-- WHEN (SELECT COUNT(*) FROM MyTable) > 100 -- not needed
BEGIN
DELETE FROM MyTable
WHERE ID NOT IN (SELECT ID
FROM MyTable
ORDER BY ID DESC
LIMIT 100);
END;

android sqlite create table size trigger

How can I create a insert trigger in sqlite? I should check if the number of rows is < 5 = ok. If the number of rows is 5 = delete first row.
Is this way of collecting garbage efficient?
The documentation is quite good: http://www.sqlite.org/lang_createtrigger.html
You should end up with something like
CREATE TRIGGER rowlimit5
AFTER INSERT ON table
BEGIN
DELETE FROM table WHERE ROWID NOT IN (
SELECT ROWID FROM table ORDER BY ROWID DESC LIMIT 5
);
END;

After deleting a row in SQLite, _ID of a new entry is not consecutive

I use this method to delete a row in my sqlite db:
db.execSQL("delete from "+TABLE_NUMS+" where _ID = '" + this.rowID + "'");
and then I update the rest of Ids to make my entries consecutive:
db.execSQL("UPDATE "+TABLE_NUMS+" set _ID = (_ID - 1) WHERE _ID > "+this.rowID);
And it works fine, but when I add new entries to my DB, the ID of the new entries still add as if the deleted entries existed, say I have 10 rows with IDs starting from 1 to 10, and then I delete number 5 and 6, the rows become 1 to 8, but the new entry's ID will be 11. So my IDs sequence would be 1 to 8 and 11. How can I fix this?
SQLite keeps track of the largest ROWID that a table has ever held using the special SQLITE_SEQUENCE table. You cam modify that sequence as:
UPDATE SQLITE_SEQUENCE SET seq = this.ID -1 WHERE name = TABLE_NUMS
The same functionality is asked in this question.
The normal ROWID selection algorithm described above will generate
monotonically increasing unique ROWIDs as long as you never use the
maximum ROWID value and you never delete the entry in the table with
the largest ROWID. If you ever delete rows or if you ever create a row
with the maximum possible ROWID, then ROWIDs from previously deleted
rows might be reused when creating new rows and newly created ROWIDs
might not be in strictly ascending order.
http://www.sqlite.org/autoinc.html
This is how SQLite works.
If you really need to have the Ids consecutive don't use autoincrement.
Insert the ids yourself.
You can select MAX(_ID) first to get the last id (greatest value).
This is because you have autoincrement set on _ID when you created the table. So, every row you add will be given a number automatically unless you explicitly set it. If it is absolutely necessary that you need the _IDs in consecutive order, I recommend that you set it yourself instead of using autoincrement.
Here is how to reset it:
delete from your_table;
delete from sqlite_sequence where name='your_table';
This will delete all your data and reset the sequence.
SQLite keeps the largest ROWID in the special SQLITE_SEQUENCE table. You can update that table as:
db.execSQL("UPDATE SQLITE_SEQUENCE SET seq = 0 WHERE NAME = '"+TABLE_NAME+"'");
OR
delete that table as:
db.delete("SQLITE_SEQUENCE","NAME = ?",new String[]{TABLE_NAME});

SQLite: Return the most recent 'n' rows per unique value of column 'y'

I have a table with the following schema:
CREATE TABLE table (
msg_id TEXT,
thread_id TEXT,
.
.
.
date INTEGER
)
I need to retrieve the most recent n msg_id per unique value of thread_id. Is there a way to do it using a single query or will I need to query the database to get the most recent distinct thread_ids, then query the database again PER unique thread_id? I recall reading somewhere that multiple database queries can get expensive.
You could use a correlated subquery. For example, for N = 5 :
select *
from YourTable yt1
where 5 <
(
select count(*)
from YourTable yt2
where yt2.thread_id = yt1.thread_id
and yt2.msg_id < yt1.thread_id
)
This is not too fast, so you might be better of with multiple queries.

Categories

Resources