471,330 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Pysqlite storing file as blob example

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')
query = 'insert into table (ID,data) values (1,?);'
result = cur.execute(query,(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)
TypeError: not all arguments converted during string formatting

I also read about using adapters, but I haven't found any clear info
on how to use them.
Does anyone have an example of storing binary data (image) in sqlite
using pysqlite?

Jul 31 '07 #1
3 7389
On Tue, 2007-07-31 at 00:25 +0000, ru*********@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
Jul 31 '07 #2
On Jul 30, 6:25 pm, rustyhow...@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() ?
>From the docs:
----
There are several built-in functions that are no longer essential to
learn, know or use in modern Python programming. They have been kept
here to maintain backwards compatibility with programs written for
older versions of Python.
....
buffer(object[, offset[, size]])
----

Jul 31 '07 #3
On Tue, 2007-07-31 at 00:13 -0700, 7stud wrote:
On Jul 30, 6:25 pm, rustyhow...@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
reply.

--
Carsten Haese
http://informixdb.sourceforge.net
Jul 31 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Juergen Gerner | last post: by
2 posts views Thread by David Horowitz | last post: by
1 post views Thread by aldonnelley | last post: by
14 posts views Thread by Nader Emami | last post: by
3 posts views Thread by Annonymous Coward | last post: by
5 posts views Thread by =?ISO-8859-1?Q?Gerhard_H=E4ring?= | last post: by
4 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.