Hi; At one point Dennis Lee Bieber helped me with the following slightly modified code:
#!/usr/bin/python import sys,os sys.path.append(os.getcwd()) import MySQLdb from login import login import re, string def printTree(aTree, level=0): tree = [] for name in sorted(aTree.keys()): tree.append("%s%s") % ("\t" * level, name) printTree(aTree[name], level + 1) def expand(fetched): aDict = {} for (name, ) in fetched: aDict[name] = {} return aDict def getChildren(levelDict, level = 0): MAXLEVEL = 7 if level > MAXLEVEL: return #possibly the data has a cycle/loop for (nm, dt) in levelDict: cursor.execute('''select c.name from categories as c inner join relationship as r on c.ID = r.Child inner join categories as p on r.Parent = p.ID where p.category = %s order by c.name''', (nm,)) levelDict[nm] = expand(cursor.fetchall()) # recursive call to do next level getChildren(levelDict[nm], level + 1) # no data return as we are mutating dictionaries in place def catTree(): user, passwd, db, host = login() database = MySQLdb.connect(host, user, passwd, db) cursor = database.cursor() cursor.execute('''create table if not exists categories (ID int(3) unsigned primary key, Category varchar(40), Parent varchar(40))''') cursor.execute('select Category, Parent from categories;') data = cursor.fetchall() cursor.execute('select Category from categories order by Parent, ID') print data Categories = [itm[0] for itm in cursor] #untuple single column if len(Categories) > 0: cursor.execute('select Parent from categories order by Parent, ID') Parents = [itm[0] for itm in cursor] MAXLEVEL = 15 cursor.execute('''create table if not exists categories (ID integer auto_increment primary key, Name varchar(40) not null, unique (Name) )''') cursor.execute('''create table if not exists Relationship (ID integer auto_increment primary key, Parent integer not null, foreign key (Parent) references categories (ID), Child integer not null, foreign key (Child) references categories (ID), check (Parent <> Child) );''') # get top level print 'ok' cursor.execute('select category from categories order by category') theTree = expand(cursor.fetchall()) getChildren(theTree) connection.commit() return printTree(theTree) else: return ['There are no categories yet.'] catTree() This throws the error: [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] Traceback (most recent call last): [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] File "/var/www/html/angrynates.com/cart/createCats.py", line 8, in ? [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] from catTree import catTree [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] File "/var/www/html/angrynates.com/cart/catTree.py", line 77, in ? [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] catTree() [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] File "/var/www/html/angrynates.com/cart/catTree.py", line 71, in catTree [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] getChildren(theTree) [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] File "/var/www/html/angrynates.com/cart/catTree.py", line 25, in getChildren [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] for (nm, dt) in levelDict: [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] ValueError: too many values to unpack There is only one category in "categories". Please advise. Victor
-- http://mail.python.org/mailman/listinfo/python-list