对于executemany(),'不是在字符串格式化期间转换的所有参数'

时间:2015-11-11 13:51:51

标签: python mysql

我想通过INSERT命令将多个数据集插入到MySQL表中。为此,我定义了以下方法:

def addRecords(self, records):
    """Add a list of shop -> filename entries into the records"""
    import pprint
    pprint.pprint(records)
    self.cursor.executemany("""
        INSERT INTO foo_records ('shop_id', 'filename', 'missing_since', 'last_reported')
        VALUES (?, ?, NOW(), NOW())
        """, records)

当我执行此操作时,我会调用TypeError: not all arguments converted during string formattingexecutemany

'shop_id'是mysql的INT(11),'filename'被定义为VARCHAR(60)。

records的内容如下:

[(1234, u'foo.csv'),
 (1234, u'bar.csv'),
 ..
 (1456, u'foobar.txt')]

我在这里缺少什么?

编辑:我在SQL上犯了一个愚蠢的错误。列名称嵌入在'中。当我删除它时,它与%s而不是?一起使用。

1 个答案:

答案 0 :(得分:0)

我认为问题在于问号。如果您使用%s(如doc指定),则会发生什么:

self.cursor.executemany("""
        INSERT INTO foo_records ('shop_id', 'filename', 'missing_since', 'last_reported')
        VALUES (%s, %s, NOW(), NOW())
        """, records)