Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pysqlite - how to save images

I need to save an image file into sqlite database in python. I could not find a solution. How can I do it?

Thanks in advance.

like image 870
Mustafa Zengin Avatar asked Jul 22 '10 14:07

Mustafa Zengin


1 Answers

write - cursor.execute('insert into File (id, name, bin) values (?,?,?)', (id, name, sqlite3.Binary(file.read())))

read - file = cursor.execute('select bin from File where id=?', (id,)).fetchone()

if you need to return bin data in web app - return cStringIO.StringIO(file['bin'])

like image 64
nazca Avatar answered Sep 22 '22 03:09

nazca