[英]Insert into MySQL table with Python
I am trying to insert basic data into my MySQL table without any success yet I'm using basic tutorials as demos. 我试图将基本数据插入我的MySQL表中,但没有成功,但我将基本教程用作演示。
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax;
mysql.connector.errors.ProgrammingError:1064(42000):您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s)' at line 1
检查与您的MySQL服务器版本相对应的手册以获取正确的语法以在第1行附近使用'%s)'
My MySQL 我的MySQL
CREATE TABLE awesome (
id int NOT NULL AUTO_INCREMENT,
test varchar(50),
PRIMARY KEY (id)
);
My Python code 我的Python代码
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="pass",
database="db"
)
mycursor = mydb.cursor()
sql = "INSERT INTO awesome (test) VALUES (%s)"
val = ("Highway 21")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
Yes the problem arises when there is only 1 argument in val tuple. 是的,当val元组中只有1个参数时,就会出现问题。 Use
采用
val=("Highway 21",)
val =(“ Highway 21”,)
Note the comma at the end. 注意最后的逗号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.