2010-05-12 113 views
0

我的問題很簡單。我有一張桌子,可以說x行,每個都有一個ID,但我不知道我有多少行。現在我想刪除我的表的最後一行...有沒有一種簡單的方法來做到這一點在android中?我一直試圖在我的where子句中使用last_insert_rowid ...但目前爲止沒有運氣... 任何想法用android的sqlite數據庫工具做到這一點?刪除表中的最後一行android

+1

你說的是表格佈局還是數據庫表格? – RoflcoptrException 2010-05-12 09:02:27

+0

你在考慮數據庫表,HTML表格還是TableLayout? – RoToRa 2010-05-12 09:03:01

+0

對不起,這是在我的腦海中。數據庫,sqlite數據庫 – Sephy 2010-05-12 09:24:20

回答

6

我假設last_insert_rowid表示你正在談論db。

DELETE FROM test WHERE id = (SELECT MAX(id) FROM test); 
+0

似乎工作。謝謝 – Sephy 2010-05-12 09:44:25

-1

我看到這篇文章,我想我可以幫助,如果任何人會有同樣的問題。

我所做的就是將我的光標移動到最後一行,並將其檢索到ID, 將它轉換爲db.delete查詢的字符串。

您可以在數據庫中的一個行,但它的ID可能是更多的則1

對於我的軟件,我用Contract類並實現BaseColumns(就像在Android開發人員網站)

希望它幫助^^

//go to the last row 
cursor.moveToLast(); 

//get the index ID of the last row 
int lastItemId = cursor.getInt(cursor.getColumnIndex(ShiftsContract.Entry.COLUMN_NAME_ID)); 

//turn it into a string 
String slastItemId = String.valueOf(lastItemId); 

//define the column from which you want to delete 
String where = ShiftsContract.Entry.COLUMN_NAME_ID + "=?"; 

//from the selected column delete the retrieved ID 
String[] args = {slastItemId}; 

db.delete(ShiftsContract.Entry.TABLE_NAME, where, args);