问题描述
因此,我有一个ListView和一个DatabaseHandler,并且我想基于长按删除一个ListView项.也应该从数据库中删除它.
So I have a ListView and a DatabaseHandler, and I want to delete a ListView item based on a long click. It should be deleted from the database too.
但是当我删除ID为1的第一项时,ListView中的新第一项将是数据库中ID为2的项.
But when I delete the first item with ID 1, the new first item in the ListView will be the item with ID 2 in the DataBase.
ListView Database
1 1
2 2
3 3
删除后
ListView Database
1 2
2 3
我想到了根据ListView ID删除数据库条目.
I thought of deleting the database entry according to the ListView ID.
delete from (select * from Student WHERE rownum = 2)
但这给我一个错误...
But it gives me an error...
我应该怎么做?谢谢!
推荐答案
您可以通过以下方法获得第(n + 1)条记录:
You can get the ( n+1 )th record by the following :
SELECT * FROM Students LIMIT 1 OFFSET n;
n是从零开始的索引.因此,您可以通过以下方式选择第二行:
n is the index you're after starting with zero. So you can select the 2nd row by:
SELECT * FROM Students LIMIT 1 OFFSET 1;
您还可以通过以下方式删除第二行:
You can also delete the second row by :
DELETE FROM Students WHERE id in (SELECT id FROM Students LIMIT 1 OFFSET 1)
这篇关于SQLITE-删除第n行(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!