uploading binary file - ODBC (SapDB)

Daniel Dittmar daniel.dittmar at sap.com
Thu Feb 20 04:13:07 EST 2003


Dan Fratean wrote:
> I am trying to find a way to upload a binary file into a field from a
> database. Can anyone give me some ideas? I can take the file, parse it
> and escape the special characters, but I don't like this idea. Better
> I would use streams, eventually a persistent ODBC connection. Can

Extrapolating the file contents into the SQL isn't a good idea as you
noticed
- there is a maximum size for SQL statements (configuration dependent)
- this doesn't work at all for UPDATE statements

Use parameters instead.

If you use mxODBC, the following should work:
data = open (fname, 'rb').read ()
cursor.execute ("insert into sometable (keycol, longcol) values (?, ?)",
[keyval, data])

I don't know if mxODBC supports the putchunk and getchunk functionality of
ODBC.

You could also use the SAPDB python modules, which allow you to pass streams
as parameters so there is no need to load the whole data into memory.

An example for the native module sapdb.sql is at
http://www.sapdb.org/7.4/longtest.py.html.

An example using the DB API compatible sapdb.dbapi:
# pass the read method, not the read data
reader = open (fname, 'rb').read
cursor.execute ("insert into sometable (keycol, longcol) values (?, ?)",
[keyval, reader])

Daniel







More information about the Python-list mailing list