2010-05-12 111 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);