From: Mignon, L. <MignonL@PCF.BE> - 2002-11-26 09:38:32
|
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!=20 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=A0: Billy G. Allie [mailto:Bil...@mu...]=20 Envoy=E9=A0: mardi 26 novembre 2002 7:40 =C0=A0: Mignon, Laurent; 'pyp...@li...'; ger...@gm... Objet=A0: Re: [Pypgsql-users] Binary data with postgress=20 Gerhard =3D?iso-8859-1?Q?H=E4ring?=3D 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: >=20 > # Database schema used: > # create table test(ba bytea, lo oid); >=20 > from pyPgSQL import PgSQL >=20 > bindata =3D "".join([chr(x) for x in range(256)] * 10) >=20 > con =3D PgSQL.connect() >=20 > cursor =3D con.cursor() >=20 > # I) BYTEA >=20 > # Storing BYTEA: > cursor.execute("insert into test(ba) values (%s)", (PgSQL.PgBytea(bindata),)) > last_oid =3D cursor.oidValue >=20 > # Retrieving BYTEA > cursor.execute("select ba from test where oid=3D%s", (last_oid,)) > row =3D cursor.fetchone() >=20 > # Check if input is the same as output > assert bindata =3D=3D row.ba >=20 > con.close() >=20 >=20 > 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=20 determining length, comparisons, concatenation (+), repetition (*), = etc. I=20 will be very happy when version 3.0 gets out the door and we can just=20 sub-class the string object :-) I will fix the class by this weekend. >>> from pyPgSQL import PgSQL >>> a =3D '\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 =3D PgSQL.connect() # Lets put it into the database >>> cu =3D 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 =3D 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 =3D cu.fetchall() >>> for i in rs: .. print i ..=20 ['\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 =3D cu.fetchall() >>> for i in rs: .. print i ..=20 ['\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 =3D=3D rs[0].a # Comparison doesn't work either Traceback (most recent call last): File "<stdin>", line 1, in ? AssertionError >>> assert a =3D=3D rs[0].a.value >>> --=20 ____ | Billy G. Allie | Domain....: Bil...@mu... | /| | 7436 Hartwell | MSN.......: B_G...@em... |-/-|----- | Dearborn, MI 48126| |/ |LLIE | (313) 582-1540 | --=20 ____ | Billy G. Allie | Domain....: Bil...@mu... | /| | 7436 Hartwell | MSN.......: B_G...@em... |-/-|----- | Dearborn, MI 48126| |/ |LLIE | (313) 582-1540 | |