RE : Binary data with postgress

"Mignon, Laurent" <[email protected]>
Newsgroups gmane.comp.python.db.pypgsql.user
Message-ID <3039D655C3E7D611985000105A15241A0C9E5A@PCF-EXCHANGE>
If I understood all what you say, the best thing to do is waiting the next
release of pypgsql to be sure that the related bug is fixed! 
Before this release, all test that I do on retrieved data must be done on
the value member of PgBytea class.

Thanks for your help!

-----Message d'origine-----
De : Billy G. Allie [mailto:[email protected]] 
Envoyé : mardi 26 novembre 2002 7:40
À : Mignon, Laurent; '[email protected]';
[email protected]
Objet : Re: [Pypgsql-users] Binary data with postgress 

Gerhard =?iso-8859-1?Q?Häring?= wrote:
> I was trying to answer this question, too. In the process of which I was
> constructing an example using BYTEA. Unfortunately, there seems to be a
> bug in pyPgSQL that we should eliminate before (finally, *cough*)
> releasing 2.3:
> 
> # Database schema used:
> # create table test(ba bytea, lo oid);
> 
> from pyPgSQL import PgSQL
> 
> bindata = "".join([chr(x) for x in range(256)] * 10)
> 
> con = PgSQL.connect()
> 
> cursor = con.cursor()
> 
> # I) BYTEA
> 
> # Storing BYTEA:
> cursor.execute("insert into test(ba) values (%s)",
(PgSQL.PgBytea(bindata),))
> last_oid = cursor.oidValue
> 
> # Retrieving BYTEA
> cursor.execute("select ba from test where oid=%s", (last_oid,))
> row = cursor.fetchone()
> 
> # Check if input is the same as output
> assert bindata == row.ba
> 
> con.close()
> 
> 
> It looks like the quoting of BYTEAs is wrong, at least when the BYTEA
> starts with a chr(0). But I haven't got a real clue where the problem
> lies. Though I vaguely remember touching that piece of code once when I
> was working on the ARRAY stuff. I hope I didn't introduce the bug then
> *blush*.

Gerhard,

The problem is with the PgBytea class. It is missing the hidden methods for 
determining length, comparisons, concatenation (+), repetition (*), etc.  I 
will be very happy when version 3.0 gets out the door and we can just 
sub-class the string object :-)  I will fix the class by this weekend.

>>> from pyPgSQL import PgSQL
>>> a = '\000ab\000'  # Create a string with NUL characters in it.
>>> len(a)
4
>>> a
'\x00ab\x00'
>>> PgSQL.PgQuoteBytea(a)  # Check how it's quoted by pyPgSQL
"'\\\\000ab\\\\000'"
>>> cx = PgSQL.connect()   # Lets put it into the database
>>> cu = cx.cursor()
>>> cu.execute('create table byteatest (a bytea, b oid)')
>>> cx.commit()
>>> cu.execute('insert into byteatest values(%s, %s)', PgSQL.PgBytea(a), 0)
>>> cx.commit()
>>> cu.execute('select * from byteatest')
>>> rs = cu.fetchone()
>>> rs
['\x00ab\x00', 0]
>>> cu.execute('insert into byteatest(a) values(%s)', PgSQL.PgBytea(a))
>>> cx.commit()
>>> cu.execute('select * from byteatest');
>>> rs = cu.fetchall()
>>> for i in rs:
..     print i
.. 
['\x00ab\x00', 0]
['\x00ab\x00', None]
>>> cu.execute('insert into byteatest(a) values(%s)', (PgSQL.PgBytea(a),))
>>> cx.commit()
>>> cu.execute('select * from byteatest');
>>> rs = cu.fetchall()
>>> for i in rs:
..     print i
.. 
['\x00ab\x00', 0]
['\x00ab\x00', None]
['\x00ab\x00', None]
>>> len(rs[0].a)	# len() doesn't work on PgBytea types
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: PgBytea instance has no attribute '__len__'
>>> len(rs[0].a.value)
4
>>> assert a == rs[0].a  # Comparison doesn't work either
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AssertionError
>>> assert a == rs[0].a.value
>>>

-- 
____       | Billy G. Allie    | Domain....: [email protected]
|  /|      | 7436 Hartwell     | MSN.......: [email protected]
|-/-|----- | Dearborn, MI 48126|
|/  |LLIE  | (313) 582-1540    |



-- 
____       | Billy G. Allie    | Domain....: [email protected]
|  /|      | 7436 Hartwell     | MSN.......: [email protected]
|-/-|----- | Dearborn, MI 48126|
|/  |LLIE  | (313) 582-1540    |




-------------------------------------------------------
This SF.net email is sponsored by: Get the new Palm Tungsten T 
handheld. Power & Color in a compact size! 
http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0002en
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.