simpli int/str problem

S

sinan .

hi all,
i have a string and int values in same dictionary like this
dict = {'str_name': 'etc' , 'int_name' : 112 }
the error occures when do this
SQL = "INSERT INTO (`AH`, `BH` ) VALUES ('" + dict['str_name'] + "',
'" + dict['int_name'] + "')"
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.
 
W

wittempj

Use substitution like below.
Hope this helps

py> d = {'str_name': 'etc' , 'int_name' : 112 }
py> SQL = "INSERT INTO (`AH`, `BH` ) VALUES ('" + d['str_name'] + "',
'" + d['int_name'] + "')"

Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
SQL = "INSERT INTO (`AH`, `BH` ) VALUES ('" + d['str_name'] + "',
'" + d['int_name'] + "')"
TypeError: cannot concatenate 'str' and 'int' objects
py> SQL = "INSERT INTO (`AH`, `BH` ) VALUES ('%s', %d)" %
(d['str_name'], d['int_name'])
py> print SQL
INSERT INTO (`AH`, `BH` ) VALUES ('etc', 112)
py>
 
P

Paul McGuire

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
this:

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
 
D

Dennis Lee Bieber

hi all,
i have a string and int values in same dictionary like this
dict = {'str_name': 'etc' , 'int_name' : 112 }
the error occures when do this
SQL = "INSERT INTO (`AH`, `BH` ) VALUES ('" + dict['str_name'] + "',
'" + dict['int_name'] + "')"
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.

Ignoring that naming a dictionary "dict" is potentially a
problem...

You don't mention which database module, since they sometimes
have different "wildcards" but most are designed to do the string
replacement IN THE .execute() CALL...

rawSQL = "insert into ('AH', 'BH') values (%s, %s)"
cursor.execute(rawSQL, (dict["str_name"], dict["int_name"]))

Some may even support dictionary replacement

rawSQL = "insert into ('AH', 'BH') values (%(str_name)s, %(int_name)s)"
cursor.execute(rawSQL, dict)

One should NOT be trying to format the data values into a static
SQL string to be fed to .execute() -- .execute() itself will determine
the quoting needed by the arguments.
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top