The UnicodeDecodeErrors are a red herring. The real problem is that
you're using string interpolation (the %-operator) to pass the image
data to the database, so your code is force-feeding binary junk directly
into the query string. This results in an SQL query that's not
syntactically correct.
In order to pass values to the database safely, you need to use
parameter binding. In this case, that'll look something like this:
cursor.execute('update products set pic1=%s where ID=1',
(MySQLdb.Binary(pic1),))
[That comma between the two closing parentheses is not a typo. Do not
leave it out!]
Note that I'm not using string interpolation to shoehorn the picture
contents into the query string. I'm passing two arguments to
cursor.execute(). The first is the query template, with a %s placeholder
for your image contents. (Note that the query template is a plain ASCII
string, so all your Unicode-related problems will disappear in a puff of
magic.) The second argument is a tuple containing the actual parameters
to be filled into the query by the database engine. This query needs
only one parameter, so I'm making a 1-tuple containing the picture
contents, wrapped inside a MySQLdb.Binary object to tell the database
that this is a binary object.
Hope this helps,
--
Carsten Haese
http://informixdb.sourceforge.net
On line 68 you have a string literal immediately followed by a tuple, so
it looks like you're trying to call a string, hence the exception. Put a
comma between the string literal and the tuple.
I know the answer, but I'm not going to spoon-feed it to you this time,
since even a mediocre programmer should be able to figure this one out,
and you really need to start thinking for yourself if you ever want to
grow as a programmer.
How much time did you spend trying to figure out what that error message
is telling you? What thought processes, if any, have you followed?
Remember that an integer is not a string and a string is not an integer.
If you are content to run your code through the Web, and iterate
through versions of it there, then that's up to you. I'd suggest using
the interactive interpreter when you can, though -- most Python
programmers that I have encountered find it extremely helpful. In
particular, it can be invaluable in examining short snippets of code
-- such as the segments that are giving you trouble -- without
rerunning the whole program.
When I am debugging errors such as this, what I typically do is snip
out the particular bit that is causing the problem and run it in the
interactive interpreter, experimenting with changes to it until it
works as I want it to. Perhaps such a method would help you in
debugging as well? Certainly it would enable you to give the list a
little more information about what you have and haven't tried when
coming here with a problem...
My guess is that you're not calling db.commit() after inserting the
second image. (If you had shown your code, I wouldn't have to guess.)
HTH,
Are you sure that you're surfing to *exactly* those URLs? When I go to
http://www.angrynates.com/cart/getpic.py?pic=2&id=1, I get an image, but
when I go to http://www.angrynates.com/cart/getpic.py?pic=1&id=2, I get
an error message. I am guessing that you have your pic and id parameter
switched around.
> Is it possible that
> because I'm passing values to the variables and perhaps simultaneously
> calling the script, that it can only fulfill the first request?
That's possible if your script is doing something stupid, but I find my
above guess much more likely.