2015-05-06 154 views
2

我试图将从我输入的值插入到MySQL数据库中。我正在使用python 3.4和mysql.connector。下面是脚本的一部分:使用Python在一个Mysql数据库中插入一个值

Value = int(input("Type a number between 1 and 10: ")) 
cursor.execute("""INSERT INTO test VALUES ('Number', Value)""") 

其实我试图找出如何得到什么,我从我的键盘插入测试表的第二场。因为它现在是它给了我一个SQL语法错误。请帮忙。提前致谢。

回答

0

使用cursor.execute绑定Value到你的查询。

cursor.execute("INSERT INTO test VALUES ('Number', %s)", (Value,)) 

这比其他答案更可取,因为它可以防止SQL注入。

0

使用字符串格式化提供价值变量

cursor.execute("""INSERT INTO test VALUES ('Number', %s)""" % Value) 
0

Value不应该有作为一个字符串。

"""INSERT INTO test VALUES ('Number', {0})""".format(Value) 
相关问题