Bottom Page

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
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
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()
    records = cursor.fetchall()

    for row in records:

        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)
        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)
    if  (connection.is_connected()):
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()
I got
Failed to insert record into MySQL table Failed executing the operation; Not enough parameters for the SQL statement
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:
--------------------------------------------------------------------------- MySQLInterfaceError Traceback (most recent call last) <ipython-input-174-703ad1ca3c2d> in <module> 50 51 cursor = connection.cursor() ---> 52 cursor.execute(mySql_insert_query, records_to_insert) 53 connection.commit() 54 print(cursor.rowcount, "Record inserted successfully into table") ~\Anaconda3\lib\site-packages\mysql\connector\ in execute(self, operation, params, multi) 246 247 if params: --> 248 prepared = self._cnx.prepare_for_mysql(params) 249 if isinstance(prepared, dict): 250 for key, value in prepared.items(): ~\Anaconda3\lib\site-packages\mysql\connector\ in prepare_for_mysql(self, params) 607 """ 608 if isinstance(params, (list, tuple)): --> 609 result = self._cmysql.convert_to_mysql(*params) 610 elif isinstance(params, dict): 611 result = {} MySQLInterfaceError: Python type tuple cannot be converted
probably because row is tuple
try records_to_insert = (str(row),sentiment_dict['compound'], sentiment) if you really want to insert the whole row tuple

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesyste jiapei100 0 131 Feb-11-2020, 01:27 PM
Last Post: jiapei100
  How to compare in python an input value with an hashed value in mysql table? Formationgrowthhacking 4 166 Jan-14-2020, 11:43 AM
Last Post: Formationgrowthhacking
  Type hinting - return type based on parameter micseydel 2 119 Jan-14-2020, 01:20 AM
Last Post: micseydel
  How to insert data if not exists in mysql? farah97 0 126 Dec-29-2019, 08:32 AM
Last Post: farah97
  MYSQL how to assign a table for each user in my username table database? YoshikageKira 1 285 Dec-26-2019, 05:57 AM
Last Post: buran
  How to insert record into MySQL using Phython abhay_kala 1 127 Dec-06-2019, 04:34 PM
Last Post: abhay_kala
  Insert a variable in a Python > Cellular AT command ElectronicsNut 1 424 Jul-07-2019, 02:26 PM
Last Post: joe_momma
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 454 May-21-2019, 11:39 AM
Last Post: avorane
  MySQL executemany - Failed executing the operation; Could not process parameters josh_marchant 1 976 May-16-2019, 04:22 PM
Last Post: woooee
  Reading in MYSQL data as integer type Daveinthebigcity 2 404 May-13-2019, 11:30 AM
Last Post: Daveinthebigcity

Forum Jump:

Users browsing this thread: 1 Guest(s)