[DB-SIG] format vs pyformat
Chris Cogdon
chris@cogdon.org
Mon, 19 Aug 2002 14:15:24 -0700
In all my database coding to date, I've been using the 'format' parameter=
=20
passing protocol to get my stuff into the database query. However, I keep=
=20
reading and hearing that 'pyformat' is by far superior. However, whenever=
=20
I've 'given it a go', it's always seemed far clunkier to me.
For example, for pyformat to work, all your variables have to be in a=20
dictionary already. Yes, this can be reasonably easy to set up. Viz:
cur.execute ( "select * from people where name ilike %(name)s and=20
age>%(age)s",
=09{ 'name':criteria.name, 'age':criteria.age } )
But I still find it much easier to use the positional parameters. Viz:
cur.execute ( "select * from people where name ilike %s and age > %s",
=09criteria.name, criteria.age )
The above is a simple example... the disparity between the two protocols=20
becomes greater with greater complexity. I understand it's possible to pa=
ss a=20
dictionary from a pre-existing namespace. Viz:
cur.execute ( "select * from people where name ilike %(name)s and=20
age>%(age)s",
=09criteria.__dict__ )
But this assumes that the values to be passed are already pre-calculated,=
or=20
you'll need to find 'temporary' space for them so you can throw them into=
the=20
dictionary. pyformat also allows reuse of parameters, but I've seen this =
used=20
too seldom to make it a big plus.
To me, 'format' parameter passing still seems a lot easier to use, but th=
ere's=20
quite a few DB connectors that support 'pyformat' over 'format': For exam=
ple,=20
PoPy only supports pyformat, while pyPgSQL supports both format and pyfor=
mat.
Am I completely missing some 'neat trick' here that would make pyformat=20
magically easy?=20
What are other people's views on the ease of using the two formats ?
Thanks in advance.
--=20
("`-/")_.-'"``-._ Chris Cogdon <chris@cogdon.org>
. . `; -._ )-;-,_`)
(v_,)' _ )`-.\ ``-'
_.- _..-_/ / ((.'
((,.-' ((,/ fL