Hi;<br>I have the following code:<br><br>import cgitb; cgitb.enable()<br>import cgi<br>import MySQLdb<br>from login import login<br>user, passwd, db, host = login()<br>db = MySQLdb.connect(host, user, passwd, db)<br>cursor= db.cursor()<br>
form = cgi.FieldStorage()<br>pic1 = form.getfirst('pic1')<br>cursor.execute('update products set pic1=%s where ID=1;' % pic1)<br><br>which throws the following error:<br><br> /var/www/html/<a href="http://angrynates.com/cart/addEdit.py">angrynates.com/cart/addEdit.py</a><br>
   81   print '<body>\n</html>'<br>   82   db.commit()<br>   83   cursor.close()<br>   84 <br>   85 add_edit()<br>add_edit = <function add_edit><br> /var/www/html/<a href="http://angrynates.com/cart/addEdit.py">angrynates.com/cart/addEdit.py</a> in add_edit()<br>
   63     id = 0<br>   64     pic1 = form.getfirst('pic1')<br>   65     cursor.execute('update products set pic1=%s where ID=1;' % pic1)<br>   66 #    while id < len(pics):<br>   67 #      cursor.execute('update products set %s=%s where ID=%s;', (colNamesPics[id], pics[id], str(id)))<br>
cursor = <MySQLdb.cursors.Cursor object>, cursor.execute = <bound method Cursor.execute of <MySQLdb.cursors.Cursor object>>, pic1 = '\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x02\x01\x00H\x00H\x00\x00\xff\xe1\x13\xd5Exif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x07\x01\x12\x00\x03\x00\x00\x00\x01...\xdb\x9f\x94\xa9\xfds\xc9y\xb1W\xda\xd66\x9azS\x84\xd1\x1f\x93\xa9\xfdG\x0fm\xe3\x84\x01\xc5\x94\xfc\x88\xcf\x06f\xc5_\x7fPS6x\x076*\xff\x00\xff\xd9'<br>
 /usr/lib64/python2.4/site-packages/MySQLdb/cursors.py in execute(self=<MySQLdb.cursors.Cursor object>, query='update products set pic1=\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x02\x01\x00H\x00H\x00\x00\xff\xe1\x13...6\x9azS\x84\xd1\x1f\x93\xa9\xfdG\x0fm\xe3\x84\x01\xc5\x94\xfc\x88\xcf\x06f\xc5_\x7fPS6x\x076*\xff\x00\xff\xd9 where ID=1;', args=None)<br>
  144         db = self._get_db()<br>  145         charset = db.character_set_name()<br>  146         query = query.encode(charset)<br>  147         if args is not None:<br>  148             query = query % db.literal(args)<br>
query = 'update products set pic1=\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x02\x01\x00H\x00H\x00\x00\xff\xe1\x13...6\x9azS\x84\xd1\x1f\x93\xa9\xfdG\x0fm\xe3\x84\x01\xc5\x94\xfc\x88\xcf\x06f\xc5_\x7fPS6x\x076*\xff\x00\xff\xd9 where ID=1;', query.encode = <built-in method encode of str object>, charset = 'latin1'<br>
<br>UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 25: ordinal not in range(128)<br>      args = ('ascii', 'update products set pic1=\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x02\x01\x00H\x00H\x00\x00\xff\xe1\x13...6\x9azS\x84\xd1\x1f\x93\xa9\xfdG\x0fm\xe3\x84\x01\xc5\x94\xfc\x88\xcf\x06f\xc5_\x7fPS6x\x076*\xff\x00\xff\xd9 where ID=1;', 25, 26, 'ordinal not in range(128)')<br>
      encoding = 'ascii'<br>      end = 26<br>      object = 'update products set pic1=\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x02\x01\x00H\x00H\x00\x00\xff\xe1\x13...6\x9azS\x84\xd1\x1f\x93\xa9\xfdG\x0fm\xe3\x84\x01\xc5\x94\xfc\x88\xcf\x06f\xc5_\x7fPS6x\x076*\xff\x00\xff\xd9 where ID=1;'<br>
      reason = 'ordinal not in range(128)'<br>      start = 25 <br><br><br>So I added UTF-8 encoding and got this error:<br><br><br>A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.<br>
 /var/www/html/<a href="http://angrynates.com/cart/addEdit.py">angrynates.com/cart/addEdit.py</a><br>   81   print '<body>\n</html>'<br>   82   db.commit()<br>   83   cursor.close()<br>   84 <br>   85 add_edit()<br>
add_edit = <function add_edit><br> /var/www/html/<a href="http://angrynates.com/cart/addEdit.py">angrynates.com/cart/addEdit.py</a> in add_edit()<br>   62     sql = 'update products set %s;' % sqlUpdate<br>   63     id = 0<br>
   64     pic1 = unicode(form.getfirst('pic1'), 'utf-8')<br>   65     cursor.execute('update products set pic1=%s where ID=1;' % pic1)<br>   66 #    while id < len(pics):<br>pic1 undefined, builtin unicode = <type 'unicode'>, form = FieldStorage(None, None, [FieldStorage('whatDo',...e('pic2', '', ''), FieldStorage('pic3', '', '')]), form.getfirst = <bound method FieldStorage.getfirst of FieldStor...('pic2', '', ''), FieldStorage('pic3', '', '')])><br>
<br>UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: unexpected code byte<br>      args = ('utf8', '\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x02\x01\x00H\x00H\x00\x00\xff\xe1\x13\xd5Exif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x07\x01\x12\x00\x03\x00\x00\x00\x01...\xdb\x9f\x94\xa9\xfds\xc9y\xb1W\xda\xd66\x9azS\x84\xd1\x1f\x93\xa9\xfdG\x0fm\xe3\x84\x01\xc5\x94\xfc\x88\xcf\x06f\xc5_\x7fPS6x\x076*\xff\x00\xff\xd9', 0, 1, 'unexpected code byte')<br>
      encoding = 'utf8'<br>      end = 1<br>      object = '\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x02\x01\x00H\x00H\x00\x00\xff\xe1\x13\xd5Exif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x07\x01\x12\x00\x03\x00\x00\x00\x01...\xdb\x9f\x94\xa9\xfds\xc9y\xb1W\xda\xd66\x9azS\x84\xd1\x1f\x93\xa9\xfdG\x0fm\xe3\x84\x01\xc5\x94\xfc\x88\xcf\x06f\xc5_\x7fPS6x\x076*\xff\x00\xff\xd9'<br>
      reason = 'unexpected code byte'<br>      start = 0 <br><br><br>So then I added UTF-16 encoding and got this error:<br><br><br>/var/www/html/<a href="http://angrynates.com/cart/addEdit.py">angrynates.com/cart/addEdit.py</a><br>
   81   print '<body>\n</html>'<br>   82   db.commit()<br>   83   cursor.close()<br>   84 <br>   85 add_edit()<br>add_edit = <function add_edit><br> /var/www/html/<a href="http://angrynates.com/cart/addEdit.py">angrynates.com/cart/addEdit.py</a> in add_edit()<br>
   62     sql = 'update products set %s;' % sqlUpdate<br>   63     id = 0<br>   64     pic1 = unicode(form.getfirst('pic1'), 'utf-16')<br>   65     cursor.execute('update products set pic1=%s where ID=1;' % pic1)<br>
   66 #    while id < len(pics):<br>pic1 undefined, builtin unicode = <type 'unicode'>, form = FieldStorage(None, None, [FieldStorage('whatDo',...e('pic2', '', ''), FieldStorage('pic3', '', '')]), form.getfirst = <bound method FieldStorage.getfirst of FieldStor...('pic2', '', ''), FieldStorage('pic3', '', '')])><br>
 /usr/lib64/python2.4/encodings/utf_16.py in decode(input=<read-only buffer ptr 0x1b66e6c4, size 25101>, errors='strict')<br>   14 <br>   15 def decode(input, errors='strict'):<br>   16     return codecs.utf_16_decode(input, errors, True)<br>
   17 <br>   18 class StreamWriter(codecs.StreamWriter):<br>global codecs = <module 'codecs' from '/usr/lib64/python2.4/codecs.pyc'>, codecs.utf_16_decode = <built-in function utf_16_decode>, input = <read-only buffer ptr 0x1b66e6c4, size 25101>, errors = 'strict', builtin True = True<br>
<br>UnicodeDecodeError: 'utf16' codec can't decode bytes in position 0-1: illegal UTF-16 surrogate<br>      args = ('utf16', '\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x02\x01\x00H\x00H\x00\x00\xff\xe1\x13\xd5Exif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x07\x01\x12\x00\x03\x00\x00\x00\x01...\xdb\x9f\x94\xa9\xfds\xc9y\xb1W\xda\xd66\x9azS\x84\xd1\x1f\x93\xa9\xfdG\x0fm\xe3\x84\x01\xc5\x94\xfc\x88\xcf\x06f\xc5_\x7fPS6x\x076*\xff\x00\xff\xd9', 0, 2, 'illegal UTF-16 surrogate')<br>
      encoding = 'utf16'<br>      end = 2<br>      object = '\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x02\x01\x00H\x00H\x00\x00\xff\xe1\x13\xd5Exif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x07\x01\x12\x00\x03\x00\x00\x00\x01...\xdb\x9f\x94\xa9\xfds\xc9y\xb1W\xda\xd66\x9azS\x84\xd1\x1f\x93\xa9\xfdG\x0fm\xe3\x84\x01\xc5\x94\xfc\x88\xcf\x06f\xc5_\x7fPS6x\x076*\xff\x00\xff\xd9'<br>
      reason = 'illegal UTF-16 surrogate'<br>      start = 0 <br><br><br>Finally, I tried UTF-32 encoding and got this error:<br><br><br>/var/www/html/<a href="http://angrynates.com/cart/addEdit.py">angrynates.com/cart/addEdit.py</a><br>
   81   print '<body>\n</html>'<br>   82   db.commit()<br>   83   cursor.close()<br>   84 <br>   85 add_edit()<br>add_edit = <function add_edit><br> /var/www/html/<a href="http://angrynates.com/cart/addEdit.py">angrynates.com/cart/addEdit.py</a> in add_edit()<br>
   62     sql = 'update products set %s;' % sqlUpdate<br>   63     id = 0<br>   64     pic1 = unicode(form.getfirst('pic1'), 'utf-32')<br>   65     cursor.execute('update products set pic1=%s where ID=1;' % pic1)<br>
   66 #    while id < len(pics):<br>pic1 undefined, builtin unicode = <type 'unicode'>, form = FieldStorage(None, None, [FieldStorage('whatDo',...e('pic2', '', ''), FieldStorage('pic3', '', '')]), form.getfirst = <bound method FieldStorage.getfirst of FieldStor...('pic2', '', ''), FieldStorage('pic3', '', '')])><br>
<br>LookupError: unknown encoding: utf-32<br>      args = ('unknown encoding: utf-32',) <br><br><br>Please advise.<br>TIA,<br>Victor<br>