mysql.connector.errors.ProgrammingError:1064(4200):您的SQL语法有错误; [英] mysql.connector.errors.ProgrammingError: 1064 (4200): You have an error in your SQL syntax;
问题描述
我正在尝试将数据插入MySQL数据库.我正在使用python 2.7和mysql.connector.
I am attempting to insert data into a MySQL database. I am using python 2.7 and I am using the mysql.connector.
我的错误是:
mysql.connector.errors.ProgrammingError:1064(4200):您有一个 您的SQL语法错误;请查看与您的MySQL服务器版本相对应的手册,以在第1行的'%s)附近使用正确的语法.
mysql.connector.errors.ProgrammingError: 1064 (4200): 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 '%s) at line 1.
这表示我尝试插入变量np(VALUES(%s));)的代码中有一个错误.np是名词短语",例如滑板".
This indicates a error in my code where I attempt to insert my variable np (VALUES (%s)");). np is a "noun-phrase" such as a "skate board".
import mysql.connector
from textblob import TextBlob
cnx = mysql.connector.connect(user='XXX', password='XXX',
host='XXXXX',
database='XXXX')
cursor = cnx.cursor(buffered=True)
Latest = ("SELECT * FROM SentAnalysis")
cursor.execute(Latest)
for row in cursor.fetchall():
SentText = row[2]
blob = TextBlob(SentText)
for np in blob.noun_phrases:
print(np)
SQLInsertCmd = ("INSERT INTO TestNounPhrase (NPhrase) VALUES (%s)")
cursor.execute(SQLInsertCmd,np)
cnx.commit()
cursor.close()
cnx.close()
手册中的示例为 https://dev.mysql.com/doc/connector-python/zh-CN/connector-python-example-cursor-transaction.html .例如
"VALUES (%s, %s, %s, %s, %s)")
我看不出有什么区别.此错误也在这里详细讨论:如何修复MySQL错误#1064? 关于stackoverflow的其他类似示例已链接到保留字,冗余逗号.但是在这些示例中,我无法发现明显的错误.
I can't see a difference. This error is also discussed in detail here : How can I fix MySQL error #1064? Other similar examples on stackoverflow have been linked to reserved words, Redundant comas .But looking at these examples I can't spot an obvious error.
任何关于我要去哪里的建议,将不胜感激.
Any suggestions on where I am going wrong would be much appreciated.
推荐答案
SQL查询中的str参数必须使用引号'str'.
因此,需要将'%s'和''用作str,将%s不将''用作数字:
str parameter in SQL query must by in quote 'str'.
So, need use '%s' with ' ' for str, and %s without ' ' for numbers:
cursor.execute("""INSERT INTO db_name (str,int,str,int)
VALUES ('%s', %s, '%s', %s)""" % (str,int,str,int))
cnx.commit()
这篇关于mysql.connector.errors.ProgrammingError:1064(4200):您的SQL语法有错误;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!