Failed to insert record into MySQL table.Python type tuple cannot be converted - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Failed to insert record into MySQL table.Python type tuple cannot be converted (/thread-23379.html) |
Failed to insert record into MySQL table.Python type tuple cannot be converted - farah97 - Dec-26-2019 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 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() RE: Failed to insert record into MySQL table.Python type tuple cannot be converted - buran - Dec-26-2019 try to change line 25 to records_to_insert = (row,sentiment_dict['compound'], sentiment)i.e. 3-element tuple, not single element list which element is tuple. also I don't think you need cursor.executemany(), just cursor.execute() RE: Failed to insert record into MySQL table.Python type tuple cannot be converted - farah97 - Dec-26-2019 I got when do this code:records_to_insert = (row,sentiment_dict['compound'], sentiment)And, when I change cursor.executemany() to just cursor.execute(). I got this error:
RE: Failed to insert record into MySQL table.Python type tuple cannot be converted - buran - Dec-26-2019 probably because row is tupletry records_to_insert = (str(row),sentiment_dict['compound'], sentiment) if you really want to insert the whole row tuple
|