所以我知道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
相关问题 更多 >
编程相关推荐