Adding Images to MySQL
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue Apr 1 13:37:44 EDT 2008
En Tue, 01 Apr 2008 09:36:00 -0300, Victor Subervi
<victorsubervi at gmail.com> escribió:
> Hi;
> I´m trying to figure out how to upload images into a MySQL database.
> (Yes,
> that is what I want to do.) I have a form that asks for data, like this:
> 1ra Foto Pequeña:
> <input type='file' name='pic1' />
> Then I send that form to a python script that processes like this:
> cursor.execute('insert into products (' + col_names + ') values (' +
> col_values + ');')
> where col_names is all the names of the columns and col_values,
> obviously,
> the values. Works fine for strings and digits. Not so well for files :)
> What
> do?
Always use bound parameters - not only it's easier and less error prone,
it's safer too. How parameters are specified depends on the DBAPI module
you're using - read the module documentation. I think MySQLdb accept
dictionary-like marks:
values = {'name': 'Red jar',
'descr': 'A nice red jar',
'pic': binary(picdata)
}
cursor.execute('''insert into products (name,description,picture)
values (%(name)s, %(descr)s, %(pic)s);''', values)
See PEP249 http://www.python.org/dev/peps/pep-0249/ and the documentation
for your database module.
--
Gabriel Genellina
More information about the Python-list
mailing list