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
#2
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()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I got
Error:
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:
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\cursor_cext.py 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\connection_cext.py 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
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 366 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  Failed attempts to load Microsoft Appstore Python DLLs piyushd 0 394 Oct-31-2023, 10:43 AM
Last Post: piyushd
  How do I stream and record at the same time with arducam? traderjoe 0 430 Oct-23-2023, 12:01 AM
Last Post: traderjoe
  Mysql and mysql.connector error lostintime 2 612 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Insert 10gb csv files into sql table via python mg24 2 1,842 Apr-28-2023, 04:14 PM
Last Post: snippsat
  Converted EXE file size is too large Rajasekaran 0 1,448 Mar-30-2023, 11:50 AM
Last Post: Rajasekaran
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 787 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  Mysql Workbench table not updated CatBall 2 1,047 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,332 Dec-27-2022, 09:43 PM
Last Post: deanhystad
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,662 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