From: | Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com> |
---|---|
To: | Frank Millman <frank(at)chagford(dot)com> |
Cc: | "psycopg(at)postgresql(dot)org" <psycopg(at)postgresql(dot)org> |
Subject: | Re: Problem with memoryview |
Date: | 2013-07-31 14:56:26 |
Message-ID: | CA+mi_8Zy50LYof=FfO9qybeQJfoLPpUXCand8CBvapS=XoCkGg@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | psycopg |
On Wed, Jul 31, 2013 at 3:08 PM, Frank Millman <frank(at)chagford(dot)com> wrote:
>
> The following refers to Python 3.3.
>
> I know that if you create a column of type 'bytea', psycopg2 accepts a
> variety of inputs, but always returns a 'memoryview'.
>
> I would like to know if it is possible, through some customisation option,
> to tell it to return 'bytes'.
Yes: you can easily create a typecaster converting bytea data to bytes:
def bytea2bytes(value, cur):
m = psycopg2.BINARY(value, cur)
if m is not None:
return m.tobytes()
BYTEA2BYTES = psycopg2.extensions.new_type(
psycopg2.BINARY.values, 'BYTEA2BYTES', bytea2bytes)
psycopg2.extensions.register_type(BYTEA2BYTES)
cur.execute("select 'abcdef'::bytea")
cur.fetchone()[0] # returns b'abcdef'
This would change the behaviour of bytea globally; you can limit the
scope of the change to a single connection or cursor passing this
object as parameter to register_type(): check the function docs for
details.
Hope it helps,
-- Daniele
From | Date | Subject | |
---|---|---|---|
Next Message | Frank Millman | 2013-08-01 05:37:19 | Fw: Problem with psycopg2, bytea, and memoryview |
Previous Message | Frank Millman | 2013-07-31 14:08:09 | Problem with memoryview |