BLOBS and python

Fredrik Lundh effbot at telia.com
Thu May 4 16:34:15 EDT 2000


sp00fD <sp00fd at my-deja.com> wrote:
> I'm trying to insert an image as a blob, but unless I escape any quotes
> ("') within it, it seems to puke.  The problem is, it's an image and if
> I escape the quotes, I change the image.  How can I do this?
>
> import cgi
> import MySQLdb
> query = cgi.FieldStorag()
> image = query["filename"].value
>
> c = MySQLdb.connect(...)
> con = c.cursor()
> con.execute("INSERT INTO foo (myblob) VALUES ('%s')" % image)

use parameters, not string formatting operators:

> con.execute("INSERT INTO foo (myblob) VALUES (?)", [image])

(the exact syntax is implementation dependent; see the docs for
your binding for details).

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list