DBI spec and parameter passing

Skip Montanaro skip at mojam.com
Mon Jan 17 18:28:23 EST 2000


    tjg> I've been reading up on DBI and what it does, and I see that when I
    tjg> have cursor for which I need to execute some SQL, I pass it some
    tjg> SQL and some parameters, can someone please show me an example of
    tjg> how that is supposed to look? Right now I build up a string using
    tjg> something like this

    tjg> 	sql = "select * from table where id='%s'" % idvar
    tjg> 	cursor.execute(sql)

    tjg> This seems to work quite well except in the in the instances where
    tjg> I have *Empty* Data fields, then mxODBC reports errors back to me
    tjg> that look like this:

Instead of doing the parameter interpolation yourself, try letting your
database module do it for you, e.g.:

    c = db.cursor()
    c.execute("""select id from city where"""
              """ city = %s and"""
              """ state = %s and"""
              """ country = %s""", (city, state, country))

Tim,

The form of your sql statements will depend on the value of the paramstyle
instance attribute of your database object.  For more details check out:

    http://www.python.org/topics/database/DatabaseAPI-2.0.html

This may not solve your zero-length string problem.  Your error message
looks more like a date formatting problem.  Still, letting your database
module do the interpolation should help minimize problems with value
formatting and migration from one database to another.

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/
847-971-7098





More information about the Python-list mailing list