1

I am trying to delete last inserted Item from database table, but always getting SQLiteException: near "ORDER" syntax error (code 1)

android.database.sqlite.SQLiteException: near "ORDER": syntax error (code 1): , while compiling: delete from pending where url = 'server' and mobile_id = '2331' and sent = '0' ORDER BY id DESC LIMIT 1

Using following query:

db.execSQL("delete from pending where url = 'server' and mobile_id = '" + string + "' and sent = '0' ORDER BY id DESC LIMIT 1");
Sophie
  • 2,594
  • 10
  • 41
  • 75

2 Answers2

3

Use nested query as follows.

db.execSQL("delete from pending where TABLE_ID = (SELECT TABLE_ID from pending where url = 'server' and mobile_id = '" + string + "' and sent = '0' ORDER BY id DESC LIMIT 1"));

TABLE_ID should be unique key id of pending table.

additionster
  • 628
  • 4
  • 14
2
DELETE FROM table
WHERE ID IN
        (
        SELECT ID
        FROM
            (
                SELECT ID
                FROM table
                WHERE url = 'server' and mobile_id = '2331' and sent = '0' 
                ORDER BY id
                DESC LIMIT 1
            ) a
        )

Note the a serves as an alias for the subquery

Nas
  • 2,158
  • 1
  • 21
  • 38