Dec-26-2019, 12:26 PM
I want to create sentiment analysis using Vader in Python and Mysql. The problem is, I got the error on mysql, especially when to insert the data into the database. The error that I got is
Error:Failed to insert record into MySQL table Failed executing the operation; Python type tuple cannot be converted
Here are the code: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()