Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51641

Re: Problem with psycopg2, bytea, and memoryview

From "Frank Millman" <frank@chagford.com>
Subject Re: Problem with psycopg2, bytea, and memoryview
Date 2013-07-31 13:43 +0200
References <ktam8i$hj3$1@ger.gmane.org> <loom.20130731T114936-455@post.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.10.1375270989.1251.python-list@python.org> (permalink)

Show all headers | View raw


"Antoine Pitrou" <solipsis@pitrou.net> wrote in message 
news:loom.20130731T114936-455@post.gmane.org...
> Frank Millman <frank <at> chagford.com> writes:
>>
>> 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'.
>>
> [...]
>>
>> 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.
>
> Just cast the result to bytes (`bytes(row[1])`). It will work both with 
> bytes
> and memoryview objcts.
>
> Regards
>
> Antoine.
>

Thanks for that, Antoine. It is an improvement over tobytes(), but i am 
afraid it is still not ideal for my purposes.

At present, I loop over a range of columns, comparing 'before' and 'after' 
values, without worrying about their types. Strings are returned as str, 
integers are returned as int, etc. Now I will have to check the type of each 
column before deciding whether to cast to 'bytes'.

Can anyone explain *why* the results do not compare equal? If I understood 
the problem, I might be able to find a workaround.

Frank


Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: Problem with psycopg2, bytea, and memoryview "Frank Millman" <frank@chagford.com> - 2013-07-31 13:43 +0200
  Re: Problem with psycopg2, bytea, and memoryview Neil Cerutti <neilc@norwich.edu> - 2013-07-31 14:08 +0000
    Re: Problem with psycopg2, bytea, and memoryview "Frank Millman" <frank@chagford.com> - 2013-08-01 10:03 +0200

csiph-web