user3116503
user3116503

Reputation: 37

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 });

Upvotes: 0

Views: 780

Answers (2)

user2839610
user2839610

Reputation:

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 });

Upvotes: 2

johnny
johnny

Reputation: 2122

    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.

Upvotes: 0

Related Questions