Error: 1210: Incorrect number of arguments executing prepared statement

Error: 1210: Incorrect number of arguments executing prepared statement

我正在尝试使用 Python 将数据插入 MySQL。

这个错误的原因是什么?

ProgrammingError: 1210: Incorrect number of arguments executing prepared statement

我的python代码:

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()

我的表:

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   

您忘记传递 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.

您的语法也有误(删除引号),请改用以下语法:

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 函数用于需要在数据库中插入多行的时候。第二个参数应该是一个列表,其中包含要插入这些不同行的值。所以要么你修改代码如下: (请注意,我在 records_to_insert 中添加了方括号 [],使其成为一个列表)

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()

让它工作的秘诀是在单值元组的末尾添加一个逗号。

示例:

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

在这种情况下:

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

它适用于单值元组。

希望对您有所帮助!