Python – Error : 1210: Incorrect number of arguments executing prepared statement

Error : 1210: Incorrect number of arguments executing prepared statement… here is a solution to the problem.

Error : 1210: Incorrect number of arguments executing prepared statement

I’m trying to insert data into MySQL using Python.

What is the cause of this error?

ProgrammingError: 1210: Incorrect number of arguments executing
prepared statement

My python code:

connection = mysql.connector.connect(host='localhost',
                         database='popsww2017',
                         user='root',
                         password='')
records_to_insert = [('---2q7vcZGU', 'Partner-provided', '35', '9s1Pvm0U8Gg8mRavZhVXdg', 'A663893851990558', '1066/2016/HDHT-Pops-Kha Ly', '1467', '0.100598')]
sql_insert_query = "INSERT INTO raw_music (`Video_ID`, `Content_Type`, `Video_Duration`, `Channel_ID`, `Asset_ID`, `Asset_Labels`, `Owned_Views`, `Partner_Revenue`) VALUES ( '%s', '% s' , '%s' , '%s', '%s' , '%s' , '%s' , '%s') "
cursor = connection.cursor(prepared=True)
result  = cursor.executemany(sql_insert_query,records_to_insert)
connection.commit()

My Table:

Video_ID    varchar(50) utf8_unicode_ci     
Content_Type    varchar(100)    utf16_unicode_ci        
Video_Duration  int(11)         
Channel_ID  varchar(100)    utf8_unicode_ci     
Asset_ID    varchar(50) utf32_unicode_ci        
Asset_Labels    varchar(400)    utf32_unicode_ci        
Owned_Views int(20)         
Partner_Revenue float   

Solution

The secret to making it work is to add a comma at the end of the single-valued tuple.

Example:

# a tuple
to_insert = ('A value to insert'**,**)

In this case:

records_to_insert = [('---2q7vcZGU', 'Partner-provided', 35, '9s1Pvm0U8Gg8mRavZhVXdg', 'A663893851990558', '1066/2016/HDHT-Pops-Kha Ly', 1467, 0.100598)**,**]

It applies to single-valued tuples.

Hope this helps!

Related Problems and Solutions