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 gio, 20/11/2008 alle 11.54 +0100, paftek ha scritto:
> >>> # Criterias to filter on, dynamically choosen by the user in the
> UI
> ... criterias = [
> ...     ('price', '>', 10),
> ...     ('quantity', '=', 50),
> ...     ('sku', '=', 'foo')
> ... ]
> >>> # Add criterias to query
> ... if criterias:
> ...     query = query + ' AND ' + ' AND '.join([
> ...         '%s %s %s' % (col, op, curs.mogrify('%s', [v]))
> ...         for col, op, v in criterias
> ...     ])
> ...

Mm..

if criterias:
    query += " AND " + " AND ".join(
        ["%s %s %%s" % (x[0], x[1]) for x in criterias])
    curs.execute(query, tuple([x[2] for x in criterias]))

It is not that mogrify isn't right; it is that using the official DBAPI
when possible is better. :)

federico

-- 
Federico Di Gregorio                         http://people.initd.org/fog
Debian GNU/Linux Developer                                [email protected]
INIT.D Developer                                           fog-NGVKUo/i/[email protected]
 In science one tries to tell people, in such a way as to be understood
  by everyone, something that no one ever knew before. But in poetry,
  it's the exact opposite.                                 -- Paul Dirac

_______________________________________________
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)

iEYEABECAAYFAkkl1GgACgkQvcCgrgZGjeslQwCgu16/A/nosptCiElFtOU9YiU5
CMYAnRJQwUzr58npCs50OQfn/07VDopi
=AX09
-----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.