2015-05-06 82 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) 
相關問題