【问题标题】:Why I got an error in SQL syntax, Failed to insert record into MySQL table 1064 (42000)?为什么我在 SQL 语法中出现错误,无法将记录插入 MySQL 表 1064 (42000)?
【发布时间】:2020-04-16 09:09:06
【问题描述】:

我想在 Python 和 Mysql 中使用 Vader 创建情绪分析。问题是,我在mysql上遇到了错误,尤其是在将数据插入数据库时​​。我得到的错误是 Failed to insert record into MySQL table 1064 (42000): You have an error in your SQL syntax;查看与您的 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()

数据库表:

【问题讨论】:

  • VALUES (%s,positive') 中,positive 上缺少一个开放引号,并且只有两个值,但 INTO 元组需要三个。
  • 是的,我已经编辑了代码,但在 'sentiment_dict['compound']' 上仍然出现错误。它无法插入数据库。为什么?

标签: python mysql


【解决方案1】:

问题出在这里:

mySql_insert_query = """INSERT INTO sent (q,polarity,senti) 
    VALUES (%s,sentiment_dict['compound'],'negative') """

这表示您正在尝试将字符串 sentiment_dict['compound'] 插入数据库,而不是它所保存的值,这很可能是错误的。您可能想要这样的东西(其他查询也类似):

mySql_insert_query = """INSERT INTO sent (q,polarity,senti) 
    VALUES (%s,%s,'negative') """

# then later

records_to_insert = [(row, sentiment_dict['compound'])]

或者更好,重构为:

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.executemany() 似乎没有必要,因为您一次只使用一组值,尽管我怀疑您首先调试的是简单案例。)

【讨论】:

  • 我已经尝试了你的第二种方法(重构),但又遇到了另一个错误,即“无法转换 Python 类型元组”。究竟是什么问题?极性为十进制数(例如:0.5678)。是这个问题的原因吗?
  • 不,%s 是占位符,不是实际的字符串请求。我不确定问题可能是什么;请您 edit 进入您的数据库架构的主要问题,以防万一那里有什么东西?
  • 已经更新了代码。我得到的最新错误是 'Failed to insert record into MySQL table Failed execution the operation; Python类型元组无法转换'
  • polarity 被声明为 int(100) (100??) 但我们传入的是一个浮点数,这可能是出现转换问题的原因?
  • 我已经把它改成了十进制 (6,4)。但是,仍然无法正常工作。我不确定是转换问题还是其他问题
猜你喜欢
  • 1970-01-01
  • 2018-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-25
  • 2019-02-21
  • 1970-01-01
相关资源
最近更新 更多