Hi;<br>I have this line of code:<br> sql = 'select Name, Price from %sPackages where ID=%s;' % (store, pid)<br>which prints to this:<br> select Name, Price from productsPackages where ID=1;<br>which when I enter it into the MySQL interpreter gives me this:<br>
mysql> select Name, Price from productsPackages where ID=1;<br>+------+--------+<br>| Name | Price |<br>+------+--------+<br>| pkg | 123.45 |<br>+------+--------+<br>1 row in set (0.00 sec)<br><br>exactly what I expect. However, in my script for some reason it returns this:<br>
((1,),)<br>Why would it do that? I guess there's some foolish thing I did in my code somewhere, but I'll be darned if I know where. Here's the whole script:<br><br>#! /usr/bin/python<br><br>import cgitb; cgitb.enable()<br>
import cgi<br>import MySQLdb<br>import sys,os<br>sys.path.append(os.getcwd())<br>from login import login<br>from template import top, bottom<br>from sets import Set<br>import fpformat<br><br>form = cgi.FieldStorage()<br>store = form.getfirst('store')<br>
cat = form.getfirst('cat', '')<br><br>user, passwd, db, host = login()<br>db = MySQLdb.connect(host, user, passwd, db)<br>cursor = db.cursor()<br><br>def displayProducts(patientID=''):<br> try: # These are stores with categories where ordering by price is important<br>
sql = 'select ID from %s where Category="%s" order by Price desc;' % (store, cat)<br> cursor.execute(sql)<br> except: # Stores, like prescriptions, where ordering by price is not important<br> sql = 'select ID from %s;' % (store)<br>
cursor.execute(sql)<br> ids = [itm[0] for itm in cursor]<br> cursor.execute('describe %s;' % store)<br> colFields, colFieldValues = [itm[0] for itm in cursor], [itm[1] for itm in cursor]<br> i = 0<br> if len(ids) > 0:<br>
for id in ids:<br># print '<tr>\n'<br> print '<form method="post" action="displayOneProduct.py">'<br> print "<input type='hidden' name='store' value='%s' />" % store<br>
print "<input type='hidden' name='id' value='%s' />" % id<br> j = 0<br> for col in colFields:<br> sql = 'select %s from %s where ID="%s";' % (col, store, str(id))<br>
cursor.execute(sql)<br> colValue = cursor.fetchone()<br> if col == 'SKU':<br> print "<input type='hidden' name='sku' value='%s' />" % colValue[0]<br>
print '<b>%s: </b>%s<br />\n' % (col, colValue[0])<br> elif col == 'Category':<br> print "<input type='hidden' name='cat' value='%s' />" % colValue[0]<br>
print '<b>%s: </b>%s<br />\n' % (col, colValue[0])<br> elif col == 'OutOfStock':<br> if colValue[0] == '1': # This product is out of stock<br> outOfStockFlag = 'yes'<br>
else:<br> outOfStockFlag = 'no'<br> elif col[:3] != 'pic':<br> notSet = 1<br> if isinstance(colValue[0], (str, int, float, long, complex, unicode, list, buffer, xrange, tuple)):<br>
pass<br> else:<br> try:<br> html = "<b>%s</b>: <select name='%s'>" % (col, col)<br> notSet = 0<br> for itm in colValue[0]:<br>
try:<br> color, number = itm.split('$')<br> html += "<option name='%s'>%s</option>" % (itm, color)<br> except:<br> html += "<option name='%s'>%s</option>" % (itm, itm)<br>
html += "</select><br />"<br> print html<br> except:<br> pass<br> if notSet == 1:<br> if len(col) > 49:<br> colX = col[:50] + '...'<br>
else:<br> colX = col<br> print '<b>%s: </b>%s<br />\n' % (colX, colValue[0])<br> elif col == 'pic1':<br> try:<br> if (colValue[0] != None):<br>
# if (colValue[0] != None) and (len(colValue[0] > 0)):<br> print '<a href="getpic.py?store=%s&pic=%s&id=%s" class="highslide" href="getpic.py?store=%s&pic=%s&id=%s" onclick="return hs.expand(this)"><img src="getpic.py?store=%s&pic=%s&id=%s" width="100" height="80" alt="" align="left" border="0" title="Click to enlarge" style="border: 0px"></a><br clear="all" />\n' % (store, col[3:], id, store, col[3:], id, store, col[3:], id)<br>
except TypeError:<br> raise<br> except:<br> raise<br># i += 1<br># try:<br># content = colValues[0][x].tostring()<br># pic = "tmp" + str(i) + ".jpg"<br>
# try:<br># os.remove(pic)<br># except:<br># pass<br># img = open(pic, "w")<br># img.write(content)<br># print '<img src="%s"><br /><br />' % pic<br>
# img.close()<br># except:<br># pass<br> j += 1<br> if store != 'prescriptions':<br> if outOfStockFlag == 'yes':<br> print '<font color="red">This item is currently <b>out of stock</b>.</font>'<br>
else:<br> print "<input type='submit' value=' More Info ' />"<br> else:<br> print "<input type='hidden' name='patientID' value='%s' />" % patientID<br>
print "<input type='submit' value=' More Info ' />"<br> print '</form><br /><br />'<br> print '</td></tr></table>\n'<br> print '</table>\n'<br>
<br>def cgiFieldStorageToDict(fieldStorage):<br> params = {}<br> for key in fieldStorage.keys():<br> params[key] = fieldStorage[key].value<br> return params<br><br>def display():<br> top()<br> dict = cgiFieldStorageToDict(cgi.FieldStorage())<br>
print dict<br> if store == 'prescriptions':<br> password = form.getfirst('password')<br> email = form.getfirst('email')<br> sql = 'select ID, FirstName, LastName from %s where PW="%s" and Email="%s";' % ('patientsPersonalData', password, email)<br>
try:<br> cursor.execute(sql)<br> patientID = [itm[0] for itm in cursor][0]<br> print "<h2>Welcome, %s %s!</h2>" % ([itm[1] for itm in cursor][0], [itm[2] for itm in cursor][0])<br>
displayProducts(patientID)<br> except:<br> print "We're sorry. The email address and password you entered do not correspond with what is recorded in our database. Please click the "back" button and try again.<br /><br />"<br>
else:<br> displayProducts()<br> cursor.execute('show tables like "%sPackages";' % store)<br> if cursor.fetchone() is not None:<br> sql = 'select ID from categories%s where Category="%s";' % (store[0].upper() + store[1:], cat)<br>
cursor.execute(sql)<br> categoryID = cursor.fetchone()[0]<br> sql = 'select p.ID from %sPackages p join %sCategoriesPackages c where c.CategoryID=%s;' % (store, store, categoryID)<br> cursor.execute(sql)<br>
packageIDs = [itm[0] for itm in cursor]<br> for pid in packageIDs:<br> sql = 'select ProductID from %sProductsPackages where PackageID=%s;' % (store, pid)<br> cursor.execute(sql)<br> productIDs = [itm[0] for itm in cursor]<br>
sql = 'select Name, Price from %sPackages where ID=%s;' % (store, pid)<br># print sql<br> print cursor.fetchall()<br> name = [itm[0] for itm in cursor]<br> price = [itm[1] for itm in cursor]<br>
print '<b>Package Name: %s</b><br />' % name<br> print 'Price: %s<br />' % price<br> allPrices = 0.00<br> for pid in productIDs:<br> sql = 'select Name, Price from %s;' % store<br>
# print sql<br> cursor.execute(sql)<br> pName, pPrice = cursor.fetchone()<br> pPrice = float(pPrice)<br> allPrices += pPrice<br> print 'Product Name: %s<br />' % pName<br>
print 'All products together usually cost: $%f<br /><br />' % str(fpformat.fix(round(int(allPrices * 100))/100, 2))<br> cursor.close()<br> bottom()<br><br>display()<br><br>TIA,<br>beno<br clear="all">
<br>-- <br>The Logos has come to bear<br><a href="http://logos.13gems.com/">http://logos.13gems.com/</a><br>