我该如何解决这个问题?


pet cannot be added 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'of birth, gender_id, cat_id, picture, instock) values ('coco','pitbull','425....' at line 1

sql = “INSERT into tblpet (姓名、品种、价格、颜色、年龄、出生日期、性别 ID、猫 ID、图片、库存) 值 (%s,%s,%s,%s,%s,%s,% s、%s、%s、%s)”
#为每列添加表单变量
val =(名称、品种、价格、颜色、年龄、dt、性别、猫、文件路径、instk)
cur.执行(sql,val)
con.commit()

我尝试过的:

我检查了所有数据库并检查了代码,但能够找到问题。

解决方案1

您有一个名为 date of birth,总是需要使用反引号进行转义:
MySQL :: MySQL 8.0 参考手册 :: 11.2 架构对象名称[^]

SQL
INSERT into tblpet (name, breed, price, color, age, `date of birth`, gender_id, cat_id, picture, instock) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)

更好的选择是重命名您的列,使其不包含空格 – 例如, date_of_birth

コメント

タイトルとURLをコピーしました