Can't Encode Pic
Victor Subervi
victorsubervi at gmail.com
Thu Nov 26 08:47:58 EST 2009
Hi;
I have the following code:
import cgitb; cgitb.enable()
import cgi
import MySQLdb
from login import login
user, passwd, db, host = login()
db = MySQLdb.connect(host, user, passwd, db)
cursor= db.cursor()
form = cgi.FieldStorage()
pic1 = form.getfirst('pic1')
cursor.execute('update products set pic1=%s where ID=1;' % pic1)
which throws the following error:
/var/www/html/angrynates.com/cart/addEdit.py
81 print '<body>\n</html>'
82 db.commit()
83 cursor.close()
84
85 add_edit()
add_edit = <function add_edit>
/var/www/html/angrynates.com/cart/addEdit.py in add_edit()
63 id = 0
64 pic1 = form.getfirst('pic1')
65 cursor.execute('update products set pic1=%s where ID=1;' % pic1)
66 # while id < len(pics):
67 # cursor.execute('update products set %s=%s where ID=%s;',
(colNamesPics[id], pics[id], str(id)))
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'
/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)
144 db = self._get_db()
145 charset = db.character_set_name()
146 query = query.encode(charset)
147 if args is not None:
148 query = query % db.literal(args)
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'
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 25:
ordinal not in range(128)
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)')
encoding = 'ascii'
end = 26
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;'
reason = 'ordinal not in range(128)'
start = 25
So I added UTF-8 encoding and got this error:
A problem occurred in a Python script. Here is the sequence of function
calls leading up to the error, in the order they occurred.
/var/www/html/angrynates.com/cart/addEdit.py
81 print '<body>\n</html>'
82 db.commit()
83 cursor.close()
84
85 add_edit()
add_edit = <function add_edit>
/var/www/html/angrynates.com/cart/addEdit.py in add_edit()
62 sql = 'update products set %s;' % sqlUpdate
63 id = 0
64 pic1 = unicode(form.getfirst('pic1'), 'utf-8')
65 cursor.execute('update products set pic1=%s where ID=1;' % pic1)
66 # while id < len(pics):
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',
'', '')])>
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0:
unexpected code byte
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')
encoding = 'utf8'
end = 1
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'
reason = 'unexpected code byte'
start = 0
So then I added UTF-16 encoding and got this error:
/var/www/html/angrynates.com/cart/addEdit.py
81 print '<body>\n</html>'
82 db.commit()
83 cursor.close()
84
85 add_edit()
add_edit = <function add_edit>
/var/www/html/angrynates.com/cart/addEdit.py in add_edit()
62 sql = 'update products set %s;' % sqlUpdate
63 id = 0
64 pic1 = unicode(form.getfirst('pic1'), 'utf-16')
65 cursor.execute('update products set pic1=%s where ID=1;' % pic1)
66 # while id < len(pics):
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',
'', '')])>
/usr/lib64/python2.4/encodings/utf_16.py in decode(input=<read-only buffer
ptr 0x1b66e6c4, size 25101>, errors='strict')
14
15 def decode(input, errors='strict'):
16 return codecs.utf_16_decode(input, errors, True)
17
18 class StreamWriter(codecs.StreamWriter):
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
UnicodeDecodeError: 'utf16' codec can't decode bytes in position 0-1:
illegal UTF-16 surrogate
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')
encoding = 'utf16'
end = 2
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'
reason = 'illegal UTF-16 surrogate'
start = 0
Finally, I tried UTF-32 encoding and got this error:
/var/www/html/angrynates.com/cart/addEdit.py
81 print '<body>\n</html>'
82 db.commit()
83 cursor.close()
84
85 add_edit()
add_edit = <function add_edit>
/var/www/html/angrynates.com/cart/addEdit.py in add_edit()
62 sql = 'update products set %s;' % sqlUpdate
63 id = 0
64 pic1 = unicode(form.getfirst('pic1'), 'utf-32')
65 cursor.execute('update products set pic1=%s where ID=1;' % pic1)
66 # while id < len(pics):
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',
'', '')])>
LookupError: unknown encoding: utf-32
args = ('unknown encoding: utf-32',)
Please advise.
TIA,
Victor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091126/4946b72d/attachment.html>
More information about the Python-list
mailing list