Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use binary data in SQLAlchemy?

Tags:

sqlalchemy

How does one use binary data (BLOB type column) in SQLAlchemy.

I just created a table with fields key, val, where val is BLOB and when I query the table, SQLAlchemy returns:

<read-only buffer for 0x83c3040, size -1, offset 0 at 0x83c3120>

How do I use this read-only buffer?

like image 232
bodacydo Avatar asked Sep 02 '25 08:09

bodacydo


1 Answers

You can iterate over it (e.g. for streaming it) or convert it to a string/binary if you want to have the whole binary in memory (which shouldn't be a problem as long as you are not dealing with movies in the database...)

>>> from sqlalchemy.util import buffer
>>> var = buffer('foo')
>>> var
<read-only buffer for 0xb727fb00, size -1, offset 0 at 0xb727fa80>
>>> str(var)
'foo'
>>> for i in var:
...   print i
... 
f
o
o

Regards, Christoph

like image 186
tux21b Avatar answered Sep 05 '25 01:09

tux21b