MySQLdb integer question

Skip Montanaro skip at pobox.com
Sun Oct 19 17:24:27 EDT 2003


    Ansgar> # sql="INSERT INTO %s (%s) VALUES %s"% (db,",".join(myfields),myvalues)

Then don't do it that way. ;-)  Try this instead:

    conn = MySQLdb.Connection(...)
    curs = conn.cursor()
    stmt = "INSERT INTO %s (%s) VALUES (%s)" % (db, ",".join(myfields),
                                                ",".join(['%s']*len(myfields)))
    curs.execute(stmt, tuple(myvalues))

The assignment to stmt results in another format string with the table name
and field names filled in and the right number of %s placeholders.  You then
pass your values as a tuple to the cursor's execute() function and let
MySQLdb perform the necessary escape trickery on the values.

Skip





More information about the Python-list mailing list