[英]Why I got an error in SQL syntax, Failed to insert record into MySQL table 1064 (42000)?
I want to create sentiment analysis using Vader in Python and Mysql.我想在 Python 和 Mysql 中使用 Vader 创建情感分析。 The problem is, I got the error on mysql, especially when to insert the data into the database.
问题是,我在 mysql 上遇到错误,尤其是在将数据插入数据库时。 The error that I got is Failed to insert record into MySQL table 1064 (42000): You have an error in your SQL syntax;
我得到的错误是 Failed to insert record into MySQL table 1064 (42000): 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 '['compound'],'negative')' at line 2. Here are the code:
检查与您的 MariaDB 服务器版本相对应的手册,了解在第 2 行的“['compound'],'negative')”附近使用的正确语法。以下是代码:
sql_select_Query = "select a from table1 union select b from table1"
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
for row in records:
print(row)
sid_obj = SentimentIntensityAnalyzer()
sentiment_dict = sid_obj.polarity_scores(row)
if sentiment_dict['compound'] >= 0.05 :
sentiment = 'positive'
elif sentiment_dict['compound'] <= - 0.05 :
sentiment = 'negative'
else :
sentiment = 'neutral'
mySql_insert_query = """INSERT INTO sent (q,polarity,senti) VALUES (%s,%s,%s) """
records_to_insert = [(row,sentiment_dict['compound'], sentiment)]
cursor = connection.cursor()
cursor.executemany(mySql_insert_query, records_to_insert)
connection.commit()
print(cursor.rowcount, "Record inserted successfully into table")
except mysql.connector.Error as error:
print("Failed to insert record into MySQL table {}".format(error))
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if (connection.is_connected()):
cursor.close()
connection.close()
The problem is here:问题在这里:
mySql_insert_query = """INSERT INTO sent (q,polarity,senti)
VALUES (%s,sentiment_dict['compound'],'negative') """
This says you are trying to insert the string sentiment_dict['compound']
, not the value it holds, into the database, which is most likely wrong.这表示您正在尝试将字符串
sentiment_dict['compound']
而不是它保存的值插入到数据库中,这很可能是错误的。 You probably want something like this (and similar for the other queries):您可能想要这样的东西(其他查询也类似):
mySql_insert_query = """INSERT INTO sent (q,polarity,senti)
VALUES (%s,%s,'negative') """
# then later
records_to_insert = [(row, sentiment_dict['compound'])]
Or better, refactor to:或者更好的是,重构为:
if sentiment_dict['compound'] >= 0.05 :
sentiment = 'positive'
elif sentiment_dict['compound'] <= - 0.05 :
sentiment = 'negative'
else:
sentiment = 'neutral'
mySql_insert_query = """INSERT INTO sent (q,polarity,senti)
VALUES (%s,%s,%s) """
records_to_insert = [(row, sentiment_dict['compound'], sentiment)]
(BTW, cursor.executemany()
seems unnecessary since you only ever use a single set of values at a time, although I suspect you are debugging the simple case first.) (顺便说一句,
cursor.executemany()
似乎没有必要,因为您一次只使用一组值,尽管我怀疑您首先调试了简单的情况。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.