Python-PySide TreeView Model 'for' statements LOGIC help needs a function maybe

60 Views Asked by At

So I know that the row in allRows has to come first so I don't repeat the main category like it is doing now. I have been trying for days now to get this right. I can't seem to get it with just if statements and for statements. I need I need to include a function to eliminate excessive iterations, and an if statement so the code doesn't break because of a category with no sub_category. I will also add two more children 'item' and 'options' once i get the first parent/child relationship down. Note: the company ID is just a simple one eventually it will become the true rootNode.

The code below is close to what I want, but still very far. It produces:

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

I have also been able to achieve

Cat1
Cat2
Cat3
Cat4
  subC4 - 1 <--correct
  subC4 - 2 <--correct

Code

    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)

This code gets this result.

 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
1

There are 1 best solutions below

0
On BEST ANSWER
    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)