[Zope] Re: [Psycopg] Uploading photos into an SQL database
Federico Di Gregorio
fog@mixadlive.com
22 Jun 2001 14:11:24 +0200
On 22 Jun 2001 12:46:30 +0100, Mayers, Philip J wrote:
> ...Never mind the why. The photo data (small - 80kb JPEGs) has to go into
> the PostgresSQL database. I'm using something like this:
>
> SQL_add_photo:
>
> insert into photo (id,data) values ( <dtml-sqlvar id type=int>, <dtml-sqlvar
> data type=string>)
>
> add_photo:
>
> container.SQL_add_photo(id,file.read())
you're sending binary data (file.read()) but tell zope to insert it as a
string. i don't even know if zope supports a type=binary switch. the
best thing you can do is to is to write a python method that directly
uses psycopg and its Binary() constructor. mmm... psycopg does not have
a Binary yet...
ok, in a python metod try out the following code (we define a Binary
class, as per DBAPI-2.0, that way you'll easily use psycopg Binary when
implemented, in a few days i hope):
import psycopg
class Binary:
def __init__(self, s):
self.s = s
def __str__(self):
return "'" + join(map(lambda x: '\\%o' % ord(x), self.s), '') +
"'"
def add_photo(id, file):
dict = {'id': int(id), 'data':Binary(file.read())}
o = psycopg.connect("your connection string here")
c = o.cursor()
c.execute("INSERT INTO photo (id, data) VALUES (%(id)d, %(data)s)",
dict)
c.commit()
hope this helps,
federico
--
Federico Di Gregorio
MIXAD LIVE Chief of Research & Technology fog@mixadlive.com
Debian GNU/Linux Developer & Italian Press Contact fog@debian.org
All programmers are optimists. -- Frederick P. Brooks, Jr.
_______________________________________________
Psycopg mailing list
Psycopg@lists.initd.org
http://lists.initd.org/cgi-bin/mailman/listinfo/psycopg