Adding Images To MySQL

Victor Subervi victorsubervi at
Sat Apr 5 16:32:00 CEST 2008

>* *(What a mess! I don't know where to begin...)

Yeah. Never claimed to be any good at this :( Just persistent :)

>* *- You say Content-Type: image/jpeg but you emit HTML code. You're lucky
if you see any

>* *text at all.

Well, I tried Content-Type: text/html and that threw an HTTP 500 Error.

>* *- HTTP 200 is not an error, it means the request was successful.

When it doesn´t execute the code, how can it be called successful? If it
doesn´t execute the code, how can you say it´s not an error? What is it,

>* *- As a general advice, try to isolate the problems. Test the database
stuff alone, in a local

>* *application. Test the cgi script alone, without database interaction.
Test the database stuff in

>* *the web server (better if you have a shell account). Merge all and test

Very good advice. Please help me understand how to do that.

This is what I have done. I have tried these:

sql = "'insert into products (" + col_names + ") values (" + val + ")', (" +
col_names + ")"



sql = "'insert into products (" + col_names + ") values (" + val + ")'"

cursor.execute(sql, (col_names,))

Neither work. However, if I print what that code spits out:

sql = "'insert into products (" + col_names + ") values (" + val + ")', (" +
col_names + ")"

print sql

then copy and paste it into a cursor.execute() statement, viola! Everything
works _just_fine_. Go figure. Why??

Incidentally, all that is without using images, and obviously since it
posted to the database after copying and pasting, I believe I am dealing
only with a python problem.

Now, concerning images, this is what I have so far.

I put an image on the server, and did this:


f =

pic1 = _mysql.escape_string(f)

It does not like this (forgot error):

pic1 = MySQLdb.Binary(f)

Escaping the string, I can successfully load this image into the database,
along with all the other fields. Now, when I load an image from the form on
the previous page with this code:

<input type='file' name='pic1' value="""

print '"', MySQLdb.Binary(data[14]), '"'

and send the form off to the next page, when I process it on that page with
this code:

pic1 = _mysql.escape_string(pic1)

print pic1

it prints out a messy binary that (almost) starts with something like ¨This
program can only be run in Win32¨, whatever that means. But a binary is
printed. (It may be an infinite binary, I stopped it after a few minutes.)

Now, if I stick it into the cursor.execute like I did above, it throws an
HTTP non-error non-posting-to-the-database 200 error. I´m more than happy to
separate all this garbage out for further testing, but I don´t know how to
do that. Your help is very much appreciated.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list