Python Forum
Failed to insert record into MySQL table.Python type tuple cannot be converted
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Failed to insert record into MySQL table.Python type tuple cannot be converted
#1
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()
Reply


Messages In This Thread
Failed to insert record into MySQL table.Python type tuple cannot be converted - by farah97 - Dec-26-2019, 12:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 475 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  Failed attempts to load Microsoft Appstore Python DLLs piyushd 0 455 Oct-31-2023, 10:43 AM
Last Post: piyushd
  How do I stream and record at the same time with arducam? traderjoe 0 489 Oct-23-2023, 12:01 AM
Last Post: traderjoe
  Mysql and mysql.connector error lostintime 2 726 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Insert 10gb csv files into sql table via python mg24 2 1,976 Apr-28-2023, 04:14 PM
Last Post: snippsat
  Converted EXE file size is too large Rajasekaran 0 1,545 Mar-30-2023, 11:50 AM
Last Post: Rajasekaran
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 853 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  Mysql Workbench table not updated CatBall 2 1,136 Feb-13-2023, 05:37 PM
Last Post: CatBall
Photo How to select NULL and blank values from MySQL table into csv python300 9 2,529 Dec-27-2022, 09:43 PM
Last Post: deanhystad
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 11,213 Dec-26-2022, 08:48 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020