SQLite: combine SELECT and DELETE in one expression

I need to run two statements:

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

I am currently preparing and launching two different operators. I wonder if there is a way to combine it in one statement.

Basically all I have to do is get the quantity column from the row before it is deleted.

+6
source share
4 answers

SQLite does not support this extension for standard SQL - you need to use both statements, first SELECT, DELETE. Of course, you can wrap them in transactions (BEGIN and COMMIT instructions guarantee before and after) to ensure atomicity and consistency.

+12
source

, / (, thread_id), think, (, "handlerid" ), null , :

update db set handlerid = <my_thread_id> where handlerid is null;
select * from db where handlerid is not null and handlerid = <my_thread_id>;
delete from db where handlerid is not null and handlerid = <my_thread_id>;

, , - , ( , ), , , , . , , , , SELECT, .

+1

, .

I want to remove this identifier where the sum = 5, so I did it

public void selectAndDelete() {


        try {
            SQLiteDatabase db = this.getWritableDatabase();
            int i=db.delete(table_name, "ID = (select ID from table_name where amount = 5)", null);

            if(i>0)
            {
                    // I'm inserting here new record
            }
        } catch (SQLException e) {

        }
    }

So in your case you need to change where the state is, as you want

0
source

You can do this by separating the two statements with a semicolon, for example. (using the SQLite .NET server):

using (SQLiteConnection conn = new SQLiteConnection("Data Source=fie.db3"))
{
  conn.Open();

  using (var cmd = conn.CreateCommand())
  {
    cmd.CommandText = "SELECT id FROM fies; DELETE FROM fies WHERE id = 5;";
    using (var reader = cmd.ExecuteReader())
    {
      while (reader.Read())
      {
        Console.WriteLine(reader[0]);
      }
    }
  }
}
-2
source

Source: https://habr.com/ru/post/1720039/


All Articles