Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51635
| From | "Frank Millman" <frank@chagford.com> |
|---|---|
| Subject | Problem with psycopg2, bytea, and memoryview |
| Date | 2013-07-31 11:44 +0200 |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4.1375263849.1251.python-list@python.org> (permalink) |
Hi all
I don't know if this question is more appropriate for the psycopg2 list, but
I thought I would ask here first.
I have some binary data (a gzipped xml object) that I want to store in a
database. For PostgreSQL I use a column with datatype 'bytea', which is
their recommended way of storing binary strings.
I use psycopg2 to access the database. It returns binary data in the form of
a python 'memoryview'.
My problem is that, after a roundtrip to the database and back, the object
no longer compares equal to the original.
>>> memoryview(b'abcdef') == b'abcdef'
True
>>> cur.execute('create table fmtemp (code int, xml bytea)')
>>> cur.execute('insert into fmtemp values (%s, %s)', (1, b'abcdef'))
>>> cur.execute('select * from fmtemp where code =1')
>>> row = cur.fetchone()
>>> row
(1, <memory at 0xb725f77c>)
>>> row[1] == b'abcdef'
False
>>> row[1].tobytes() == b'abcdef'
True
>>>
Using MS SQL Server and pyodbc, it returns a byte string, not a memoryview,
and it does compare equal with the original.
I can hack my program to use tobytes(), but it would add complication, and
it would be database-specific. I would prefer a cleaner solution.
Does anyone have any suggestions?
Versions - Python: 3.3.2 PostgreSQL: 9.2.4 psycopg2: 2.5
Frank Millman
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Problem with psycopg2, bytea, and memoryview "Frank Millman" <frank@chagford.com> - 2013-07-31 11:44 +0200
csiph-web