473,242 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 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 7502
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tony | last post by:
Hi, I have dynamically created a PDF document in memory as a FileOutputStream Now I have to get it into a DB2 table, storing it as a BLOB. The table has a document id, document name, some date...
6
by: Juergen Gerner | last post by:
Hello Python fans, I'm trying and searching for many days for an acceptable solution... without success. I want to store files in a database using BLOB fields. The database table has an ID field...
2
by: David Horowitz | last post by:
Hi folks, I want to be able to store and retrieve UNSAVED Word documents as BLOBs. I got all the info for storing them if they're already saved on the file system. But what if they're not...
1
by: aldonnelley | last post by:
Hi there. I'm a long-time lurker and (I think) first time poster. Only relatively new to python, and I'm trying to get pysqlite to work with binary data, and having a tough time of it. I want...
14
by: Nader Emami | last post by:
I have installed "TurboGears" and I would install 'pysqlite' also. I am a user on a Linux machine. If I try to install the 'pysqlite' with 'easy_install' tool I get the next error message. The...
4
by: lorirobn | last post by:
Hi, I need to add photos to my database. Back End is on MS SQL Server (I believe 2000), and Front End is on MS Access. I have read about storing the photos as BLOBS, but I am not sure how to...
3
by: Annonymous Coward | last post by:
I remember readng that BLOBs can be stored externally (with reference to the BLOB file stored in tables instead). Does anyone have any experience doing this ? I have a few questions: 1).what...
5
by: =?ISO-8859-1?Q?Gerhard_H=E4ring?= | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 pysqlite 2.5.0 released ======================= I'm pleased to announce the availability of pysqlite 2.5.0. This is a release with major new...
4
by: Astley Le Jasper | last post by:
I've been getting errors recently when using pysqlite. I've declared the table columns as real numbers to 2 decimal places (I'm dealing with money), but when doing division on two numbers that...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.