how to store binary file data to a database blob

Greg Gaughan beta at thinksql.co.uk
Thu May 27 19:30:05 EDT 2004


Your DB-API driver should allow you to pass binary data easily via a typed
parameter to execute (or executemany for multiple rows). For example, in
ThinkSQL, you can say:

    blobdata=open('image.jpg', 'rb').read()
    s1.execute("INSERT INTO picture_table (image) VALUES (?)",
(ThinkSQL.Binary(blobdata),))

Regards,
Greg Gaughan
www.thinksql.co.uk


"David Stockwell" <winexpert at hotmail.com> wrote in message
news:mailman.363.1085669805.6949.python-list at python.org...
> Hi,
>
> I'd like to read the contents of a file into memory.  The problem is that
> this file is binary.  I then want to store the whole thing in memory to a
> database as a blob.
> I have no problem reading from a file:  I did this:
>
> import os
> f = open('/bin/ls' , 'r+b')
> data = f.read()
> f.close()
>
> How do I place that in a blob?   If I concatenate this to a sql string,
how
> will the database sql parser know where my data string ends, knowing that
my
> data string could contain lots of ' and " and other little things in it.
???
>
> storeString = "insert into mytable (filedata) values ( %s ) " % data
>
> Does that look right?  I don't think it would execute correctly.
>
> Thanks
>
>
> David Stockwell






More information about the Python-list mailing list