Hi; I'm using python 2.4.3 which apparently requires that I import Set: from sets import Set I've done this. In another script I successfully manipulated MySQL sets by so doing. Here's the code snippet from the script where I was able to call the elements in a for loop:
if isinstance(colValue[0], (str, int, float, long, complex, unicode, list, buffer, xrange, tuple)): pass else: try: html = "<b>%s</b>: <select name='%s'>" % (col, col) notSet = 0 for itm in colValue[0]: try: color, number = itm.split(':') html += "<option name='%s'>%s</option>" % (itm, color) except: html += "<option name='%s'>%s</option>" % (itm, itm) However, when I try that in my current script, the script fails. It throws no error, but rather just quits printing to the screen. Here's the code snippet: elif types[x][0:3] == 'set': for f in field: print '<td>%s</td>\n' % (field) else: print '<td>%s</td>\n' % (field) Notice that I can slice to determine if it's a set (I've printed something after that call to be sure). But once I try to loop through the set it quits printing to screen. The entire code follows. TIA, beno #! /usr/bin/python import MySQLdb import cgi import sys,os sys.path.append(os.getcwd()) from login import login from sets import Set def enterProducts2(): print '''Content-type: text/html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> </head> <body> <form enctype='multipart/form-data' action='enterProducts3.py' method='post'> ''' form = cgi.FieldStorage() store = form.getfirst('store') print "<input type='hidden' name='store' value='%s' />" % store user, passwd, db, host = login() count = 0 count2 = 0 db = MySQLdb.connect(host, user, passwd, db) cursor = db.cursor() cursor.execute('select ID from %s;' % store) test = cursor.fetchall() if len(test) > 0: cursor.execute('show columns from %s;' % store) colNames = [itm[0] for itm in cursor] types = [itm[1] for itm in cursor] colNamesWithCommas = ', '.join(colNames) try: cursor.execute('select ID from %s;' % store) ids = cursor.fetchall() if store == 'prescriptions': cursor.execute('show tables like "%PersonalData";') personalDataTables = [itm[0] for itm in cursor] else: personalDataTables = [] print '<h1>%s</h1>\n<table><tr><td>\n' % (store[0].upper() + store[1:]) print '<h3>What do you want to do?</h3>\n<table><tr><td>\n' print "<input type=radio name='whatDo' value='insert' />Add<br />\n" print "<input type=radio name='whatDo' value='update' />Update<br />\n" print "<input type=radio name='whatDo' value='delete' />Delete<br />\n" print '</td></tr></table>\n<br /><br />\n' print '<table border=1>\n' print '<tr>\n' print '<th align=center><b>Check</b></th>\n' i = 0 while i < len(colNames): if i == 0: # This is the ID field print '<th align=center><b>', colNames[i], '</b></th>\n' try: cursor.execute('describe relationships%s;' % (store[0].upper() + store[1:])) relationshipsDescription = cursor.fetchall() cursor.execute('select * from relationships%s where %sID="%s";' % (store[0].upper() + store[1:], store[0].upper() + store[1:], ids[0][0])) relationshipFields = cursor.fetchone() j = 0 for relDescrip in relationshipsDescription: if j != 0: # Skip the store ID print '<th><b>%s Name</b></th>\n' % (relDescrip[0][:-2]) j += 1 except: pass # There are no relationships else: print '<th align=center><b>', colNames[i], '</b></th>\n' i += 1 print '</tr>\n' j = 0 z = 3 a = 0 for id in ids: a += 1 j += 1 for d in id: bg = ['#ffffff', '#d2d2d2', '#F6E5DF', '#EAF8D5'] z += 1 print '<tr bgcolor="%s">' % bg[z % 4] cursor.execute('select * from %s where ID=%s;' % (store, str(d))) col_fields = cursor.fetchall() col_fields = col_fields[0] tmp = [] for field in col_fields: tmp.append(field) col_fields = [] for field in tmp: col_fields.append(field) i = 0 x = 0 y = 0 w = 0 for field in col_fields: if colNames[x] == 'SKU': sku = field if colNames[x][:3] == 'pic': y += 1 w += 1 print '<td><input type="hidden" name="%s" />\n' % str(x) # im = Image.open(getpic) # width, height = im.size() # for infile in sys.argv[1:]: # outfile = os.path.splitext(infile)[0] + "_thumb.jpg" # if infile != outfile: # try: # im = Image.open(infile) # im.thumbnail((128, (width/128)*height), Image.ANTIALIAS) # im.save(outfile, "JPEG") # except IOError: # print "cannot create thumbnail for ", infile # print '<img src="%s"><br /><br /></td>\n' % (outfile) print '<img src="getpic.py?store=%s&pic=%d&sku=%s" width="100"></td>\n' % (store, w, sku) count2 += 1 else: if x == 0: # This is the ID field d = id[0] check = 'check' + str(d) i += 1 print '<td><input type="checkbox" name="', check, '" value="', field, '" /></td>\n' if field == 'ID': print '<td>%s</td>\n' % (field) if field != 'ID': print '<td>%s</td>\n' % (field) if personalDataTables != []: i = 0 for relField in relationshipFields: if i != 0: # The first value is the same as d sql = 'select FirstName, LastName from %sPersonalData where ID="%s";' % (relationshipsDescription[i][0][:-2].lower(), relField) cursor.execute(sql) names = cursor.fetchone() print '<td>%s %s</td>\n' % (names[0], names[1]) i += 1 elif types[x][0:3] == 'set': for f in field: print '<td>%s</td>\n' % (field) else: print '<td>%s</td>\n' % (field) x += 1 print '</tr><tr>\n' print '<td align=center colspan=', len(colNames) + 1, '>\n' print '</td></tr></table>\n' print '<input type="hidden" name="count" value="%s" />\n' % count2 print '<input type="submit" value=" Send " />\n' except: print 'There is no data yet. Please add a product.\n<br /><br />' print "<input type='hidden' name='whatDo' value='insert' />\n" print '<input type="submit" value=" Add " />\n' else: print 'There is no data yet. Please add a product.\n<br /><br />' print "<input type='hidden' name='whatDo' value='insert' />\n" print '<input type="submit" value=" Add " />\n' print ''' </form> </body> </html> ''' cursor.close() enterProducts2()
-- http://mail.python.org/mailman/listinfo/python-list