2014-09-13 30 views
1

我想從SQLite數據庫表中刪除最後一行,但不斷收到錯誤。有人可以找到我的語法錯誤嗎?當試圖從SQLite數據庫中刪除最後一行時出現語法錯誤(code1)

找到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

子查詢必須始終在括號括起來:

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

我在開始時丟失了一個支架!非常感謝! – ErinG 2014-09-16 03:16:29

相關問題