sqlite数据库示例:
**我不创建数据库,它是android bookmarks数据库,我使用contentproviders访问它。
**据我在一些搜索后了解,sqlite不支持外键约束,但触发器是可能的-http://www.sqlite.org/cvstrac/wiki?p=ForeignKeyTriggers
文件夹列->0=不是文件夹(false),1=文件夹(true)
父列->保存其文件夹的ID

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,我想循环查找它的所有内部项目(更多文件夹和项目)。
例如-我有id=1,所以我可以通过请求查询其父项id=1的所有项来轻松删除ids 2和id 3,获取项id,然后删除。
问题是我不知道如何删除id=4及其内部项(item1、item2和folder3)和folder3内部项。
希望能帮上忙!已经试了几个小时了:\
谢谢!

最佳答案

如果在Java中,我想,像findNextChildId这样的函数不应该是个问题:

 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");
    }
  }
}

关于java - 递归删除SQLite项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7947904/

10-09 06:21
查看更多