Passing variable into MySQL using Python 2.7 -


i've been trying insert string value variable in python 2.7 mysql statement. can't seem work, point me in right direction?

import mysqldb  country_name = raw_input("which country like?\n")  dbh = mysqldb.connect(host="localhost",                       user="boole",                       passwd="****",                       db="mysql_experiment_1") sth = dbh.cursor() sth.execute("""select name, population world name=(%s)""", (country_name)) row in sth:     print row[0], row[1] 

it outputs:

/usr/bin/python2.7 "/home/boole/documents/python scripts/mysql_experiment_1/main.py" country like? canada traceback (most recent call last):   file "/home/boole/documents/python scripts/mysql_experiment_1/main.py", line 10, in <module>     sth.execute("""select name, population world name=(%s)""", (country_name))   file "/usr/local/lib/python2.7/dist-packages/mysqldb/cursors.py", line 187, in execute     query = query % tuple([db.literal(item) item in args]) typeerror: not arguments converted during string formatting  process finished exit code 1 

thanks, boole

try

cursor.execute("select name, population world name = %s", [country_name]) 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -