Python MySQLdb字符串替换

Python MySQLdb字符串替换,python,mysql,Python,Mysql,我试图弄明白为什么我的mysql insert语句似乎不能与字符串替换一起工作。简单的例子 #!/usr/bin/python import MySQLdb db = MySQLdb.connect(host="localhost", user="***", passwd="***", db="***") cur = db.cursor() a = '1' b = '2' c = '3' cur.execute(""" INSERT INTO rented_users (username,log

我试图弄明白为什么我的mysql insert语句似乎不能与字符串替换一起工作。简单的例子

#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect(host="localhost", user="***", passwd="***", db="***")
cur = db.cursor()
a = '1'
b = '2'
c = '3'
cur.execute(""" INSERT INTO rented_users (username,login,password) VALUES ('1','2','3')    """)
cur.execute(""" INSERT INTO rented_users (username,login,password) VALUES (%s,%s,%s);""", (a,b,c)

mysql> describe rented_users;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| username | varchar(100) | YES  |     | NULL    |       |
| login    | varchar(100) | YES  |     | NULL    |       |
| password | varchar(100) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
一组3行(0.00秒)

基本上,我的第一条insert语句工作正常,但当我尝试使用字符串替换的第二条insert语句时,它失败了。它也没有给我一个非常描述性的错误

File "insert_test", line 10

                                                                                                    ^
SyntaxError: invalid syntax
奇怪的是,完全相同的语法似乎在不同的脚本中工作。真的很困惑。如蒙协助,将不胜感激

干杯

缺少结束语


下一行可能会报告无效语法。之所以会出现这种情况,是因为Python语法允许您在下一行中添加括号,只要其中只有空格。

谢谢Thomas,我知道这很简单,不知道为什么它在我的其他脚本中工作。奇怪的不过,谢谢你。
cur.execute(""" INSERT INTO rented_users (username,login,password) VALUES (%s,%s,%s);""", (a,b,c)