Python PySide TreeView模型“for”语句逻辑帮助可能需要一个函数

Python PySide TreeView模型“for”语句逻辑帮助可能需要一个函数,python,for-loop,sqlite,treeview,model-view,Python,For Loop,Sqlite,Treeview,Model View,所以我知道allRows中的行必须排在第一位,所以我不会像现在这样重复主要类别。几天来我一直在努力把这件事做好。我似乎无法用if语句和for语句来理解它。我需要包含一个函数来消除过度迭代,以及一个if语句,这样代码就不会因为没有子类的类别而中断。一旦我得到第一个父/子关系,我还会再添加两个子项“item”和“options”。注意:公司ID只是一个简单的ID,最终它将成为真正的根节点 下面的代码接近我想要的,但仍然很遥远。它产生: Cat1 subC1 - 1 <--correct

所以我知道allRows中的行必须排在第一位,所以我不会像现在这样重复主要类别。几天来我一直在努力把这件事做好。我似乎无法用if语句和for语句来理解它。我需要包含一个函数来消除过度迭代,以及一个if语句,这样代码就不会因为没有子类的类别而中断。一旦我得到第一个父/子关系,我还会再添加两个子项“item”和“options”。注意:公司ID只是一个简单的ID,最终它将成为真正的根节点

下面的代码接近我想要的,但仍然很遥远。它产生:

Cat1
  subC1 - 1 <--correct
  subC1 - 2 <--correct
Cat2
  subC1 - 1 <--wrong
  subC1 - 2 <--wrong
Cat3
  subC1 - 1 <--wrong
  subC1 - 2 <--wrong
Cat4
  subC1 - 1 <--wrong
  subC1 - 2 <--wrong
Cat1
  subC2 - 1 <--wrong
  subC2 - 2 <--wrong
Cat2
  subC2 - 1 <--correct
  subC2 - 2 <--correct
Cat3
  subC2 - 1 <--wrong
  subC2 - 2 <--wrong
Cat4
  subC2 - 1 <--wrong
  subC2 - 2 <--wrong
Cat1
  subC3 - 1 <--wrong
  subC3 - 2 <--wrong
Cat2
  subC3 - 1 <--wrong
  subC3 - 2 <--wrong
Cat3
  subC3 - 1 <--correct
  subC3 - 2 <--correct
Cat4
  subC3 - 1 <--wrong
  subC3 - 2 <--wrong
Cat1
  subC4 - 1 <--wrong
  subC4 - 2 <--wrong
Cat2
  subC4 - 1 <--wrong
  subC4 - 2 <--wrong
Cat3
  subC4 - 1 <--wrong
  subC4 - 2 <--wrong
Cat4
  subC4 - 1 <--correct
  subC4 - 2 <--correct
这段代码得到这个结果

 rootNode   = Node("Categories")

    self.dbCursor.execute("SELECT * FROM Category WHERE company_ID=1")
    allRows = self.dbCursor.fetchall()

    self.dbCursor.execute("SELECT cat_ID from Category WHERE company_ID=1")
    cat_ids = self.dbCursor.fetchall()

    self.dbCursor.execute("SELECT * from Sub_Category")
    sub_ids = self.dbCursor.fetchall()



    for row in allRows:
        row = Node(row[2], rootNode)
        for sub in sub_ids:
            sub = Node(sub[2], row)

Cat1
  all subcats
Cat2
  all subcats
Cat3
  all subcats
Cat4
  all subcats
    rootNode   = Node("Categories")

    self.dbCursor.execute("SELECT * FROM Category WHERE company_ID=1")
    allRows = self.dbCursor.fetchall()

    self.dbCursor.execute("SELECT cat_ID from Category WHERE company_ID=1")
    cat_ids = self.dbCursor.fetchall()

    self.dbCursor.execute("SELECT category_ID from Sub_Category")
    sub_ids = self.dbCursor.fetchall()


    for x in cat_ids:
            self.dbCursor.execute("SELECT * FROM Sub_Category WHERE category_ID=(?)",(x))
            subNames = self.dbCursor.fetchall()
            print x
            for row in allRows:
                row = Node(row[2], rootNode)
                for sub in subNames:
                    print sub[1]
                    print x

                    sub = Node(sub[2], row)
 rootNode   = Node("Categories")

    self.dbCursor.execute("SELECT * FROM Category WHERE company_ID=1")
    allRows = self.dbCursor.fetchall()

    self.dbCursor.execute("SELECT cat_ID from Category WHERE company_ID=1")
    cat_ids = self.dbCursor.fetchall()

    self.dbCursor.execute("SELECT * from Sub_Category")
    sub_ids = self.dbCursor.fetchall()



    for row in allRows:
        row = Node(row[2], rootNode)
        for sub in sub_ids:
            sub = Node(sub[2], row)

Cat1
  all subcats
Cat2
  all subcats
Cat3
  all subcats
Cat4
  all subcats
    rootNode = Node("Categories")

    self.dbCursor.execute("SELECT * FROM Category WHERE company_ID=1")
    allCatRows = self.dbCursor.fetchall()

    self.dbCursor.execute("SELECT cat_ID FROM Category WHERE company_ID=1")
    cat_ids = self.dbCursor.fetchall()

    self.dbCursor.execute("SELECT subCat_ID FROM Sub_Category")
    subCat_ids = self.dbCursor.fetchall()

    self.dbCursor.execute("SELECT item_ID FROM Item")
    item_ids = self.dbCursor.fetchall()

    for ids in cat_ids:
        self.dbCursor.execute("SELECT * FROM Sub_Category WHERE cat_ID=(?)", (ids))
        allSubRows = self.dbCursor.fetchall()

        for cat in allCatRows:

            if cat[0] in ids:
                cat = Node(cat[2], rootNode)

                for sids in subCat_ids:
                    self.dbCursor.execute("SELECT * FROM Item WHERE subCat_ID=(?)", (sids))
                    allItemRows = self.dbCursor.fetchall()

                    for sub in allSubRows:

                        if sub[0] in sids:
                            sub = Node(sub[2], cat)

                            for tids in item_ids:
                                self.dbCursor.execute("SELECT * FROM Option WHERE item_ID=(?)", (tids))
                                allOptionRows = self.dbCursor.fetchall()

                                for item in allItemRows:

                                    if item[0] in tids:
                                        item = Node(item[2], sub)

                                        for option in allOptionRows:
                                            option = Node(option[2], item)