[Tutor] From SQL Blobs to Python Buffers to Actual Files

Steven D'Aprano steve at pearwood.info
Fri Jun 4 06:18:44 CEST 2010


On Fri, 4 Jun 2010 10:57:07 am GoodPotatoes wrote:

> I have been given a legacy database, and need to read the binaryfiles
> out to a disk.  The table has columns "filename" and "binaryFile",
> where the binaryFile is a BLOB
>
> My python script so far is:
>
> import pyodbc
> cnxn=pyodbc.Connection("DSN=sybasedatabase")
> cursor=cnxn.cursor()
> p=cursor.execute("select top 1 * from FOO..Table").fetchone()
> #p contains ('UsersOldFile.rtf', <read-only buffer for 0x010E1540,
> size 1496, offset 0 at 0x010C04E0>)
>
> #I tried to write this out to the disk as:
> myfile=open(p[0],'wb')
> myfile.write(p[1])
> myfile.close()
>
> #but all I get is gibberish.  Is there another way to handle the
> buffer, or something else I'm missing?

What do you mean "gibberish"? I would expect a binary blob to look 
exactly like random binary characters, in other words, gibberish, so 
what makes you think this is not working perfectly?

What do you get if you print the binary blob, and what do you expect to 
get?



-- 
Steven D'Aprano


More information about the Tutor mailing list