简体   繁体   English

为什么我在 SQL 语法中出现错误,无法将记录插入 MySQL 表 1064 (42000)?

[英]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()

Database table:数据库表: 在此处输入图片说明

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.

相关问题 更新表记录失败:1064 (42000):您的 SQL 语法有错误; Python - Failed to update table record: 1064 (42000): You have an error in your SQL syntax; Python Python MySQL, 1064 (42000), “您的 SQL 语法有错误;...” - Python MySQL, 1064 (42000), "You have an error in your SQL syntax;..." 无法将记录插入到table_sensor_log表中-语法错误-无法将记录插入到表中-42000 - Failed inserting record into table_sensor_log table - Syntax error - failed inserting record to table - 42000 错误:'1064 (42000):您的 SQL 语法有错误 - Error: '1064 (42000): You have an error in your SQL syntax 我曾尝试使用带有条目小部件的 tkinter 将记录插入到 sql 表中,但出现 sql 语法错误 - I've been tried to insert a record to sql table using tkinter with entry widget, but i got an sql syntax error 1064 (42000):您的 SQL 语法有错误; - 1064 (42000): You have an error in your SQL syntax; 连接到 MySQL 1064 (42000) 尝试插入时出错 - Error while connecting to MySQL 1064 (42000) While trying to insert mysql.connector.errors.ProgrammingError: 1064 (42000): 你的 SQL 语法有错误; 在第 1 行的“)”附近使用正确的语法 - mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; for the right syntax to use near ')' at line 1 1064(42000):您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法使用 - 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use 从 MySQL 表 1064 读取数据时出错 (42000) - Error reading data from MySQL table 1064 (42000)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM