Error: 1210: Incorrect number of arguments executing prepared statement

Viewed 4336

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 codes:

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 tables:

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   
3 Answers

the secret to make it work it's 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 works for single valued tuples.

I hope it helps!

You forgot to pass the executemany method parameters:

result  = cursor.executemany(sql_insert_query,records_to_insert)

MySQLCursor.executemany() Method Syntax:

cursor.executemany(operation, seq_of_params)

This method prepares a database operation (query or command) and executes it against all parameter sequences or mappings found in the sequence seq_of_params.

Also your syntax is wrong (remove quotes), use the following instead:

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)

executemany function is used when you need to insert many rows in the database. And the second parameter should be a list containing values to be inserted in these different rows. So either you modify the code to the following: (Note that I have added square brackets [] to records_to_insert, making it a list)

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' , %d , '%s', '%s' , '%s' , %d , %f) "
cursor = connection.cursor(prepared=True)
result  = cursor.executemany(sql_insert_query, records_to_insert)
connection.commit()
Related