SQLiteException:near" ORDER"在编译时

时间:2016-12-06 08:02:43

标签: android sqlite android-sqlite

我试图从数据库表中删除最后插入的Item,但总是得到SQLiteException:near" ORDER"语法错误(代码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

使用以下查询:

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

2 个答案:

答案 0 :(得分:3)

使用嵌套查询,如下所示。

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应该是挂起表的唯一键ID。

答案 1 :(得分: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
        )

注意 a 用作子查询的别名

相关问题