Re: About mogrify

Federico Di Gregorio <fog-NGVKUo/i/[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Organization initd.org
Message-ID <[email protected]>
Il giorno lun, 22/09/2008 alle 12.54 +0200, Oswaldo Hernández ha
scritto:
> Hello,
> 
> I have a special case in i need to use mogrify() before call execute.
> 
> I try:
> 
>  >>> con = pg.connect('dbname=sc user=postgres')
>  >>> sql = u"select id, desc from tabla where desc = %s"
>  >>> params = ("aaa",)
>  >>>
>  >>> sqlmog = con.cursor().mogrify(sql, params)
> Traceback (most recent call last):
>    File "<input>", line 1, in <module>
> NotSupportedError: unicode queries not yet supported

Why do you need to call mogrify here?

> To solve the error (I'm using wx unicode version) i encode the sql string using the encoding used in 
> the connection: 'sql = sql.encode(con.encoding)'
> 
> . Question 1:
> Is this the correct way to do it?

Yes. This is something that psycopg should do by itself and will be
added first or later. In the meantime converting from unicode to the
connection encoding is the right thing to do.

> . Question 2:
> In an old message (01/30/2007) Federico says:
>  > is there for DEBUG ONLY. There's really no reason to use .mogrify() directly; .execute() and
>  > psycopg2.extensions.adapt() should be enough for almost any need.
> 
> It's safe the use of mogrify() directly?

mogrify() is currently safe but you should not use it anyway because
there is no guarantee that it will _always_ be safe. .execute()
and .adapt() will never change semantics and .mogrify() is there only
because one sometimes need to see a query without looking at the
database logs (like if you don't have direct shell access to a remote
server). In theory you don't need .mogrify() at all: if you feel like
you need it, please explain why and I'll try to find a better way to do
the same thing or, if mogify() is really needed I'll fix psycopg API to
support what you need.

federico

-- 
Federico Di Gregorio                         http://people.initd.org/fog
Debian GNU/Linux Developer                                [email protected]
INIT.D Developer                                           fog-NGVKUo/i/[email protected]
  Those who do not study Lisp are doomed to reimplement it. Poorly.
                                     -- from Karl M. Hegbloom .signature

_______________________________________________
Psycopg mailing list
Psycopg-IAPFreCvJWPBWskQ1e/[email protected]
http://lists.initd.org/mailman/listinfo/psycopg
signature.asc (application/pgp-signature, 197 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkjXetwACgkQvcCgrgZGjeu+RgCgqAWqxseLamWrRf+CkBc6AVzH
Eb8AoMYKmwpJzcHX4adkiQBXPB/sz/97
=PDEK
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.