DBI spec and parameter passing

Skip Montanaro skip at mojam.com
Tue Jan 18 11:31:08 EST 2000


    Marc> Note that one should always use binding parameters if possible:
    Marc> the driver or interface usually knows better how to encode the
    Marc> data according to the DBs specs and it's also much easier to write
    Marc> and more portable.

Marc,

In my local case (MySQLdb), I use "%s" and your indication is that ODBC uses
"?", which is fine.  Other database modules apparently use named
interpolation (e.g., "%(city)s").  Is there a database-independent way of
building the basic strings other than switching on the value of
db.paramstyle:

    db = somethingDBIcompliant(...)
    c = db.cursor()
    if db.paramstyle == "qmark":
        c.execute("sql statement with ? embedded", params)
    elif db.paramstyle == "numeric":
        c.execute("sql statement with :1 embedded", params)
    elif db.paramstyle == "format":
        c.execute("sql statement with %s embedded", params)
    ...

or is that much portability worth the effort?  My code stops before this
point in its attempts to be independent of a particular database module.

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