Re: booleans

Daniele Varrazzo <[email protected]> Sun, 29 Aug 2010 21:54:19 +0100
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
On Sat, Aug 28, 2010 at 2:52 PM, Garry Saddington
<garry-5i1fM1l6rxR3vm4NAeV6PtBc4/[email protected]> wrote:
> Psycopg2 when used in Zope via the zope adapter seems to return booleans as
> True or False. Is there any way to make it return 1 or 0 as psycopg1 did?


Yes, you can write an adapter to convert the postgres boolean type
into the python values 1 and 0:

>>> INTBOOL = psycopg2.extensions.new_type(
    psycopg2.extensions.BOOLEAN.values,
    "INTBOOL",
    lambda value, curs, _map={'t':1,'f':0, None:None}: _map[value])

>>> psycopg2.extensions.register_type(INTBOOL)
>>> cur.execute("select 't'::boolean, 'f'::boolean, null::boolean;")
>>> cur.fetchone()
(1, 0, None)

See http://initd.org/psycopg/docs/advanced.html#type-casting-from-sql-to-python
for details.

-- Daniele