Storing files in a BLOB field via SQL

Jonathan M. Franz jfranz at neurokode.com
Mon Jun 7 23:54:09 CEST 2004


>cur = conn.cursor()
>cur.execute("INSERT INTO table (order,data) VALUES (?,?)", (order, data))

>and the DBAPI/Database driver takes care of the rest.

No... it may sometimes, but you really should use .setinputsizes() with the Types defined in the module so that the DBAPI module and driver have some clue of what you're doing.  Not all dbapi modules are very smart about types, so its a good idea to get in the habit of doing .setinputsizes() and .setoutputsizes(). In the above example, the module doesn't know the difference between a normal string and a python string object holding a binary string... so you should do the following before the execute:

cur.setinputsizes(dbapi_module.STRING, dbapi_module.BINARY)

This assumes 'order' is a string... sub NUMBER if it is a numeric value.

Alternately, if data is wrapped in via the Binary() constructor, this step could be skipped, ie:

EncData = dbapi_module.Binary(data)
# now use EncData where data was in the .execute() call

PS: this was typed in a hurry: I hope it helps, but it might have errors.






More information about the Python-list mailing list