我一直在尝试将Python 2.7中的变量中的字符串值插入到MySQL语句中。 我似乎无法让它发挥作用,有人能指出我正确的方向吗?
import MySQLdb
country_name = raw_input("Which country would you like?\n")
dbh = MySQLdb.connect(host="localhost",
user="boole",
passwd="****",
db="mySQL_Experiment_1")
sth = dbh.cursor()
sth.execute("""SELECT name, population FROM world WHERE name=(%s)""", (country_name))
for row in sth:
print row[0], row[1]
输出:
/usr/bin/python2.7 "/home/boole/Documents/Python Scripts/mySQL_Experiment_1/main.py"
Which country would you 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 FROM world WHERE 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) for item in args])
TypeError: not all arguments converted during string formatting
Process finished with exit code 1
谢谢, 布尔
答案 0 :(得分:1)
试试这个
@timestamp