Pysqlite storing file as blob example
carsten at uniqsys.com
Tue Jul 31 14:49:26 CEST 2007
On Tue, 2007-07-31 at 00:13 -0700, 7stud wrote:
> On Jul 30, 6:25 pm, rustyhow... at gmail.com wrote:
> > I'm trying to store binary data in a sqlite database and call into the
> > db using pysqlite 3.
> > What I've got so far is this:
> > import sqlite
> > con = sqlite.connect(DB_PATH)
> > cur = con.cursor()
> > query = """create table t1(
> > ID INTEGER PRIMARY KEY,
> > data BLOB );"""
> > cur.execute(query)
> > con.commit()
> > b = buffer('/path/to/binary/file')
> buffer() ?
Using buffer is not in itself wrong, as some DB-API modules, including
sqlite3, do use buffer objects to encapsulate binary data. However, the
encapsulation should be called in a portable way by using the standard
Binary factory function: b = sqlite3.Binary(contents).
What is wrong, though, is passing the filename into it instead of the
contents. This is in addition to the wrong execute call and using the
wrong version of the sqlite module, as I pointed out in my previous
More information about the Python-list