我找不到这段代码的错误:
sql = "INSERT INTO diff (val) VALUES (%s)"
test = '99'
mycursor.execute(sql, test)
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 '%s)' at line 1
谁能给我解释一下吗?提前致谢!
最佳答案
如果您能描述您正在使用哪个数据库连接器,将会有所帮助,但您可以尝试以下一些操作:
如果该值是字符串,请尝试用引号将 %s 括起来
"INSERT INTO diff (val) VALUES ('%s')"
您可能需要让执行函数的第二个参数是一个元组,即
mycursor.execute(sql, (test,))
您的连接器还可能支持关键字参数
mycursor.execute("INSERT INTO diff (:val)", {'val': '99})
关于python - Python 上的 SQL 语法不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53346780/