Skip site navigation (1) Skip section navigation (2)

Re: getquoted and unicode

From: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
To: "Carl S(dot) Yestrau Jr(dot)" <carl(at)featureblend(dot)com>
Cc: psycopg(at)postgresql(dot)org
Subject: Re: getquoted and unicode
Date: 2011-01-23 12:44:36
Message-ID: AANLkTiketPhAv_nO4OrHHPxpzmGmE9UuqFjQgKiEaViH@mail.gmail.com (view raw or flat)
Thread:
Lists: psycopg
On Sun, Jan 23, 2011 at 7:53 AM, Carl S. Yestrau Jr.
<carl(at)featureblend(dot)com> wrote:
> I'm currently using psycopg2.extensions.adapt without a connection
> intentionally.
>
> The following results in a: psycopg2.InterfaceError: can't encode
> unicode string to latin-1
>
> import psycopg2
>
> psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
> adapted = psycopg2.extensions.adapt(u'test')
> adapted.getquoted()

You are right, it should work, and it works indeed in the psycopg dev
version, but it looks broken in 2.3.2 and probably in previous
versions.

I can see that the glitch happens when the adapter is not "prepared"
for the connection: if you add that step it will work. Preparation
always happens when the parameters are passed to the query.

    >>>  cnn = psycopg2.connect('dbname=test')
    >>> adapted.prepare(cnn)
    >>> adapted.getquoted()
    "E'test'"

Preparation is probably not mentioned in the docs at all: this is
something else to review. If you are into writing an adapter, you may
want to take a look to SQL_IN or the hstore adapter for complete
examples.

If you need to work without the connection, do you want to try
checking out my dev branch? I've just pushed it on
<https://github.com/dvarrazzo/psycopg>.

Cheers,

-- Daniele

In response to

Responses

psycopg by date

Next:From: Carl S. Yestrau Jr.Date: 2011-01-23 17:17:16
Subject: Re: getquoted and unicode
Previous:From: Karsten HilbertDate: 2011-01-23 11:54:26
Subject: Re: getquoted and unicode

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group