[DB-SIG] BLOB data types

M.-A. Lemburg mal at egenix.com
Sat Jan 1 12:31:21 CET 2005

Andy Dustman wrote:
> On Fri, 31 Dec 2004 18:53:52 -0800, Robert Brewer <fumanchu at amor.org> wrote:
>>Andy Dustman wrote:
>>>What's the current view on the best object class to use for
>>>Binary/BLOB objects? PEP-249 recommends buffer, but buffer
>>>appears deprecated. MySQLdb used to strings, but currently
>>>uses array. mxODBC appears to use strings. psycopg appears
>>>to use buffer.
>>Won't Python strings be encoded, and therefore often choke on values between 128 and 255? To my naive mind, unicode objects would be the preferred choice.
> No. With MySQL, at least, the only meta-characters it cares about in
> strings passed to it are single-quote ('), backslash (\), and NUL
> (zero-byte), and all of these can be escaped with backslash, and there
> is an API function to do this safely. You don't have to do this with
> values you pass in; it happens automatically with MySQLdb. Getting
> 8-bit binary strings out of MySQL and into Python is not a problem
> either.
> One reason Unicode objects weren't used because, when PEP-249 was
> written, they didn't exist (IIRC).

Unicode is only meant for text data. Until we get a proper
binary type in Python, I think strings or buffers are still
the best way to go. For input, using a buffer object will
make it possible for the DB-API interface to differ between
text data (strings) and binary data (buffer) and apply
any necessary action when binding paramaters.

This is what mxODBC does, BTW, in addition to being careful
with non-ASCII string data in strings (not all databases can
store binary in CHAR or VARCHAR columns).

Happy New Year,
Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Jan 01 2005)
 >>> Python/Zope Consulting and Support ...        http://www.egenix.com/
 >>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
 >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
2004-12-06: Released eGenix mx Extensions for Python 2.4

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::

More information about the DB-SIG mailing list