<font size="2">
<div>Hi;</div>
<div>I have the following code which produces a file every time I need to display an image from MySQL. What garbage! Surely, python is capable of better than this, but the last time I asked for help on it, I got no responses. Is this only possible with custom classes? Please, give me some guidance here!</div>
</font><font face="Courier New" size="2">
<p>try:</p>
<p>  w += 1</p>
<p>  getpic = "getpic" + str(w) + ".py"</p>
<p>  try:</p>
<p>    os.remove(getpic)</p>
<p>  except:</p>
<p>    pass</p>
<p>  code = """</p>
<p>#!/usr/local/bin/python</p>
<p>import cgitb; cgitb.enable()</p>
<p>import MySQLdb</p>
<p>import cgi</p>
<p>import sys,os</p>
<p>sys.path.append(os.getcwd())</p>
<p>from login import login</p>
<p>user, passwd, db, host = login()</p>
<p>form = cgi.FieldStorage()</p>
<p>picid = int(form["id"].value)</p>
<p>x = int(form["x"].value)</p>
<p>pics = {1:'pic1',2:'pic1_thumb',3:'pic2',4:'pic2_thumb',5:'pic3',6:'pic3_thumb',7:'pic4',8:'pic4_thumb',\</p>
<p>9:'pic5',10:'pic5_thumb',11:'pic6',12:'pic6_thumb'}</p>
<p>pic = pics[x]</p>
<p>print 'Content-Type: text/html'</p>
<p>db = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)</p>
<p>cursor= db.cursor()</p>
<p>sql = "select " + pic + " from products where id='" + str(picid) + "';"</p>
<p>cursor.execute(sql)</p>
<p>content = cursor.fetchall()[0][0].tostring()</p>
<p>cursor.close()</p>
<p>print 'Content-Type: image/jpeg'</p>
<p>print</p>
<p>print content</p>
<p>"""</p>
<p>  script = open(getpic, "w")</p>
<p>  script.write(code)</p>
<p>  print '<td><input type="hidden" name="%s"' % str(x), ' value="%s">' % pic</p>
<p>  print '<img src="%s?id=%d&x=%d"><br /><br /></td>\n' % (getpic, d, y)</p></font><font size="2">
<div> </div>
<div>TIA,</div>
<p>Victor</p></font>