尝试从SQLite数据库中删除最后一行时的语法错误(code1)

时间:2014-09-13 16:56:33

标签: android sqlite delete-row

我试图从SQLite数据库表中删除最后一行,但我一直收到错误。有人能找到我的语法错误吗?

找到MAXID

private String MAXID = COLUMN_ID + "=" + "SELECT MAX("+COLUMN_ID+") FROM "+TABLE_NAME+")";

DELETE Staement

public void deleteLastRow(){
    try{
        ourDatabase.delete(TABLE_NAME, MAXID, null);

    }
    catch(Exception e){
        e.printStackTrace();
        Log.e("Deleteing last row", e.toString());
    }
}

错误日志

 09-13 12:53:14.570: E/Deleteing last row(2045): android.database.sqlite.SQLiteException: near "SELECT": syntax error (code 1): , while compiling: DELETE FROM gradest WHERE _id=SELECT MAX(_id) FROM gradest)

1 个答案:

答案 0 :(得分:1)

子查询必须始终括在括号中:

DELETE FROM gradest WHERE _id = (SELECT MAX(_id) FROM gradest)