simpli int/str problem

Paul McGuire ptmcg at
Fri Aug 12 17:02:01 CEST 2005

I just did this sort of thing the other day!

Your database only accepts ints for BH, but remember, you are building
an SQL *string* to be executed.  To show SQL that your BH value is an
int, not a string, do not enclose it in quotes.

(Another style hint: don't name dict's "dict", as this will mask the
actual type name.  Let's try "vDict" for now, meaning "value dict".)

SQL = "INSERT INTO XYZ('AH', 'BH' ) VALUES ('" + vDict['str_name'] + \
       "', " + str(vDict['int_name']) + ")"

In my program, I found it a bit easier to follow if I used string
interpolation (the string % operation), and named format fields.  Try

SQL = "INSERT INTO XYZ('AH', 'BH' ) VALUES ('%(str_name)s',
%(int_name)d)" % vDict

Again, note that the string value is surrounded by quotes, but the
integer value is not.

Also, you will need to replace XYZ with the actual table name. :)

-- Paul

More information about the Python-list mailing list