在试图将json数据存入mysql时,出现了错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 

总结原因是由于json数据没有用引号的问题,并且必须是单引号
原代码

insert into `food_info` (`food`,`difficult`)
VALUES ([\"鸡胸肉:500克\", \"奥尔良腌肉料:30克\"] ,  普通 )

修改后

insert into `food_info` (`food`,`difficult`)
VALUES ('[\"鸡胸肉:500克\", \"奥尔良腌肉料:30克\"]' ,  '普通')

如果是通过变量插入数据

insert into `food_info` (`food`,`difficult`) VALUES ('%s') % value_str
"insert into hello ('food_info') (`food`,`difficult`) VALUES ('{value}')".format(value=value)

参考文章https://www.jb51.net/article/149024.htm

Logo

更多推荐