Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite: Combine SELECT and DELETE in one statement

Tags:

sql

sqlite

I need to run two statements like so:

Select amount from db where ID=5
DELETE from db where ID=5

Currently I prepare and run two different statements. I wonder if there is a way to combine it in one statement.

Basically all I need to do is to get an amount column from the row before it is deleted.

like image 642
leon Avatar asked Oct 14 '09 04:10

leon


People also ask

How do I delete multiple records in SQLite?

I used this: String[] Ids = new String[]{"1", "2", "3"}; mContext. getContentResolver(). delete(CONTENT_URI, ID + " IN (?, ?, ?)",Ids);

How do you delete a record in SQLite explain with example?

SQLite DELETE Query is used to delete the existing records from a table. You can use WHERE clause with DELETE query to delete the selected rows, otherwise all the records would be deleted.

How use like delete in SQL?

select * from mytable where myvalue like 'findme%'; then replacing "select *" with "delete" should delete them. delete from mytable where myvalue like 'findme%'; That should work with any SQL database, as long as you have sufficient permissions.


2 Answers

SQLite does not support this extension to standard SQL -- you do have to use both statements, SELECT first, DELETE next. You can wrap them in a transaction, of course, (BEGIN and COMMIT statements before and after will guarantee that), to guarantee atomicity and consistency.

like image 116
Alex Martelli Avatar answered Oct 03 '22 02:10

Alex Martelli


If you just want to select rows and delete them in one pass, you can use the returning clause in the delete statement.

For instance:

delete from myTable returning *

The delete statement has all select functionalities possible such as with and where that permits to select rows with any logic.

like image 28
gerardnico Avatar answered Oct 03 '22 00:10

gerardnico