递归删除SQLite的项目 [英] Recursive delete of SQLite items
问题描述
SQLite的分贝例如:
SQLite db example:
**我没有创建数据库,这是Android的书签分贝,这是我访问使用contentproviders。
** I don't create the db, it's Android Bookmarks db, which I access using contentproviders.
**从我一些搜索后了解到,SQLite不支持外键约束,但触发器是可能的 - 的 http://www.sqlite.org/cvstrac/wiki?p=ForeignKeyTriggers
** from what I understand after some searches, SQLite doesn't support foreign key constraints but triggers are possible - http://www.sqlite.org/cvstrac/wiki?p=ForeignKeyTriggers
文件夹栏 - > 0 =不是一个文件夹(假),1 =一个文件夹(真)
FOLDER column -> 0 = not a folder (false), 1 = a folder (true)
父栏 - >持有其夹在 ID
PARENT column -> holds the ID of its folder
ID TITLE FOLDER PARENT
1 folder1 1 0
2 item1 0 1
3 item2 0 1
4 folder2 1 1
5 item1 0 4
6 item2 0 4
7 folder3 1 4
8 item1 0 7
9 item2 0 7
...等
在我的Android应用程序,我试图递归删除一个SQLite数据库项目。
我的第一个文件夹的ID,我想循环查找所有它的内部项目(更多的文件夹和项目)。
On my Android app, I'm trying to recursively delete items from a SQLite db. I have the ID of the first folder and I want to loop to find all it's inner items (more folders and items).
例如 - 我有ID = 1,这样我就可以很容易地通过要求所有项目的查询,他们的父母是ID = 1删除ID的2和3,得到物品ID,然后删除
For example - I have ID=1, so I can easily delete IDs 2 and 3 by requesting a query of all items their parent is ID=1, get items IDs and then delete.
问题是我无法弄清楚如何删除ID = 4,其内部项目(项目1,项目2,和folder3),然后folder3内的物品。
The problem is I can't figure out how to delete ID=4 and its inner items (item1, item2, and folder3) and then folder3 inner items.
很想一些帮助这个!一直试图小时:\\
Would love some help with this! been trying for hours :\
谢谢!
推荐答案
如果在Java中则是这样的,我认为,我认为像 findNextChildId
不应该是一个问题的功能
If in java then something like this I think, I think functions like findNextChildId
should not be a problem:
deleteFolderAndSub(int id){
int childId = findNextChildId(id);
//Let -1 be that there are no more children
while (childId!=-1){
if (checkIfFolder(childId)){
deleteFolderAndSub(childId);
//on exiting from above function folder should be empty so it can be deleted without problem
deleteEmptyFolder(id);
} else {
deleteitem(id);
}
//it should be while loop and we have to find another child inside the loop
childId = findNextChildId(id)
}
}
findNextChildId(int parentId){
Coursor yourCoursor;
while(yourCoursor.moveToNext()){
if (parentId==yourCoursor.getInt("PARENT")){
yourCoursor.getInt("ID");
}
}
}
这篇关于递归删除SQLite的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!