繁体   English   中英

Python Mysql 插入 - 元组索引超出范围

[英]Python Mysql Insert - tuple index out of range

这是我遇到问题的代码片段

for x in myresult:
    sql = "INSERT INTO `writing_correction` (`autoinc`, `q_usage_id`, `ruleId`, `message`, `replacements`, `offset`, `errorLength`, `category`, `ruleIssueType`) VALUES (NULL, %d, %s, %s, %s, %d, %d, %s, %s )"
#    sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
#    sql = "INSERT INTO `writing_correction` (`autoinc`, `q_usage_id`, `ruleId`, `message`, `replacements`, `offset`, `errorLength`, `category`, `ruleIssueType`) VALUES (NULL, '100', 'ruleid', 'message', 'replacemenet', '12', '3', 'cat', 'ruleissuetype')"
#    val = ("John", "Highway 21")
#    mycursor.execute(sql, val)
    print(x[3])
    matches = tool.check(x[3])
    for y in matches:
#        sql = "INSERT INTO `writing_correction` (`autoinc`, `q_usage_id`, `ruleId`, `message`, `replacements`, `offset`, `errorLength`, `category`, `ruleIssueType`) VALUES (NULL, %d, %s, %s, %s, %d, %d, %s, %s )" % ( x[0], y.ruleId, y.message, y.replacements, y.offset , y.errorLength, y.category, y.ruleIssueType )
        val = ( [ x[0] ], (y.ruleId), (y.message), (y.replacements), [y.offset] , [y.errorLength] , (y.category), (y.ruleIssueType) )
        print(val)
#        mycursor.execute(sql , ( x[0], y.ruleId, y.message, y.replacements, y.offset , y.errorLength, y.category, y.ruleIssueType ) )
        mycursor.executemany(sql, val)

注释代码是我尝试使其工作的尝试和错误尝试,但由于某种原因它无法正常工作。

目前我收到以下错误:

File "/usr/local/lib/python3.8/dist-packages/mysql/connector/cursor.py", line 75, in __call__
    return bytes(self.params[index])
IndexError: tuple index out of range

val应该是一个元组数组。 每个元组对应一行。 所以一次填写数组,然后executemany一次。

所以:

val = []
for y in matches:
    val.append(  ( x[0], y.ruleId, y.message, y.replacements, y.offset , y.errorLength , y.category, y.ruleIssueType ) )

print(val)

mycursor.executemany(sql, val)

参考: executemany

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM