PythonPySide树视图模型'for'语句逻辑帮助需要函数mayb

2024-09-29 19:21:53 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我知道allRows中的行必须排在第一位,这样我就不会像现在这样重复主类别了。我已经试了好几天了。我似乎不能用if语句和for语句来表达。我需要包含一个函数来消除过多的迭代,还有一个if语句,这样代码就不会因为没有子类别的类别而中断。我还将添加两个子'项目'和'选项'一旦我得到第一个父/子关系下来。注意:公司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

我也能做到

Cat1
Cat2
Cat3
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 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

Tags: selfidforexecuteselectrowcategorywrong
1条回答
网友
1楼 · 发布于 2024-09-29 19:21:53
    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)

相关问题 更多 >

    热门问题