我写在DB直接与蟒蛇光标对象插入脚本错误与Python光标
cursor.execute("INSERT INTO info ([height], [weight],[type]) VALUES (%s,%s,%s)" % (height, manufacturerid, weight))
有时候,我没有为“重”一些,它抛出一个错误:
Incorrect syntax near ','. (102) (SQLExecDirectW)")
如何处理这样的错误?
我写在DB直接与蟒蛇光标对象插入脚本错误与Python光标
cursor.execute("INSERT INTO info ([height], [weight],[type]) VALUES (%s,%s,%s)" % (height, manufacturerid, weight))
有时候,我没有为“重”一些,它抛出一个错误:
Incorrect syntax near ','. (102) (SQLExecDirectW)")
如何处理这样的错误?
有了尝试,除了看到python文档:https://docs.python.org/2/tutorial/errors.html
你不应该使用字符串格式化的SQL查询。
相反的:让这些在层更容易得到处理
cursor.execute("INSERT INTO info ([height], [weight], [type]) VALUES (%s,%s,%s)" %(height, manufacturerid, weight))
使用
cursor.execute("INSERT INTO info ([height], [weight], [type]) VALUES (%s,%s,%s)", (height, manufacturerid, weight))
可能会解决您的问题,并不会受到SQL注入或问题,像一个你正拥有的。
因为这似乎是甲骨文,我没有与Python使用它,请参阅文档,但PEP 249所指出参数化查询的占位符: https://www.python.org/dev/peps/pep-0249/#paramstyle
与文档根据,你应该永远不会做这样的查询(你的样子):
cursor.execute("INSERT INTO info ([height], [weight],[type]) VALUES (%s,%s,%s)" % (height, manufacturerid, weight))
你应该这样做如下:
cursor.execute("INSERT INTO info ([height], [weight],[type]) VALUES (%s,%s,%s)" , (height, manufacturerid, weight))
检查this获得更多帮助。