Pysqlite storing file as blob example

Carsten Haese carsten at uniqsys.com
Tue Jul 31 03:09:22 CEST 2007


On Tue, 2007-07-31 at 00:25 +0000, rustyhowell 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()mean
> query = """create table t1(
>             ID INTEGER PRIMARY KEY,
>             data    BLOB );"""
> cur.execute(query)
> con.commit()
> b = buffer('/path/to/binary/file')
> query = 'insert into table (ID,data) values (1,?);'
> result = cur.execute(query,(b))

The second argument to execute() must be a sequence of parameter values.
You have one parameter value, the string b. To make a one-tuple, you
need a comma, as in "(b,)".

> con.commit()
> 
> The error message I get is :
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/lib/python2.5/site-packages/sqlite/main.py", line 255, in
> execute
>     self.rs = self.con.db.execute(SQL % parms)

This traceback line worries me. I'm almost certain that you're not using
the latest version of the sqlite module. The module that is included
with Python2.5 is called sqlite3, and it lives
in .../python/site-packages/sqlite3/*
and .../python/lib-dynload/_sqlite3.so.

HTH,

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list