python操作mysql插入字符串,json报错:.ProgrammingError: (1064, 'You have an error in your SQL syntax; check the m
问题描述
执行sql语句的时候老是报错,原来不能在执行的字符串里面添加format,传给数据库的时候回转义,无论你用单引号,双引号,三引号,都没有用,怎么改都没有用
解决方法
在涉及变量的地方用格式化符号代替,在execute函数里面传入对应的变量
代码示例:
sql_insert = 'INSERT INTO 热搜 VALUES (%s, %s, %s, %s, %s, %s)'
cursor = conn.cursor()
row = cursor.execute(sql_insert,(url,title,hot_num,topics,concern_and_view,time_snap))
conn.commit()