pysqlite - how to save images

Mustafa Zengin picture Mustafa Zengin · Jul 22, 2010 · Viewed 15.1k times · Source

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.

Answer

nazca picture nazca · Jul 22, 2010

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'])