simpli int/str problem

Scott David Daniels Scott.Daniels at Acm.Org
Fri Aug 12 14:08:22 EDT 2005


Paolino wrote:
> sinan . wrote:
> 
>> hi all,
>> i have a string and int values in same dictionary like this
>> dict = {'str_name': 'etc' , 'int_name' : 112 }
Bad idea to call this "dict" -- that is a system-defined name
for the dictionary type. I'll use "holder" below.

>> the error occures when do this
>> SQL = "INSERT INTO (`AH`, `BH` ) VALUES ('" + dict['str_name'] + "',
>> '" + dict['int_name'] + "')"
^^ I suspect this should be:
     SQL = "INSERT INTO TABLENAME(AH, BH) VALUES (...
>> cursor.execute(SQL)
>> python does not accep dict['int_name'] in SQL variable but when i
>> convert this variable to the str , python accepts but i cannot insert
>> that into database because database only accept int in `BH `
>> thanks.

> Try use:
> SQL = "INSERT INTO (`AH`, `BH` ) VALUES 
> ('%s,%d)"%(dict['str_name'],dict['int_name'])
This will work, but the DB interface allows you to leave the
variable formatting to it.  Depending on the parameter style
your DB interface supports, you may use at least one of:

     cursor.execute("INSERT INTO TABLENAME(AH, BH) VALUES(?, ?)",
                    [(holder['str_name'], holder['int_name'])])
or
     cursor.execute("INSERT INTO TABLENAME(AH, BH) VALUES(:1, :2)",
                    [(holder['str_name'], holder['int_name'])])
     ### I think, it _might_ be ...(0, :1)",
or
     cursor.execute("INSERT INTO TABLENAME(AH, BH) "
                    "VALUES(:str_name, :int_name)",
                    holder)

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list