如何通过Python向MySQL行添加两个blob

2024-09-27 17:33:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在可以使用下面的代码将BLOb插入到行中代码段:-你知道吗

TheData = open("/home/mjh/Documents/DemoData/sjp.bin", 'rb').read()

sql = "Insert into Results (idResults, idClient, TestDateTime, ResultBinary) 
    Values (10, 7, '2014-11-05 14:09:11', %s)"

cursor.execute(sql, (TheData,))

但是,我想将多个blob放在同一行中,并将代码扩展为:-

sql = "Insert into Results (idResults, idClient, TestDateTime, ResultBinary, SecondResult) 
    Values (10, 7, '2014-11-05 14:09:11', %s, %s)"

cursor.execute(sql, (TheData, SecondData,))

这就产生了错误:-你知道吗

_mysql_exceptions.OperationalError: (1241, 'Operand should contain 1 column(s)')

在我看来,这是一个逻辑上的变化,它基于扩展insert来添加其他字段类型。这是否意味着我必须先插入(第一个BLOb),然后更新(第二个BLOb)?你知道吗


Tags: 代码executesql代码段cursorblobresultsinsert
3条回答

我发现将Execute行更改为:-

你知道吗游标.执行(sql,([TheData,SecondData]))

成功了。我会尝试其他方法

您是否尝试过使用命名参数,即:

sql = "Insert into Results (idResults, idClient, TestDateTime, ResultBinary, SecondResult) 
    Values (10, 7, '2014-11-05 14:09:11', %(one)s, %(two)s)"

cursor.execute(sql, { 'one': TheData, 'two': SecondData})

Reference

我已经检查过它对我有效。将多个字段值传递给sql execute函数没有错。错误是您可能会通过python列表。您应该将其转换为字符串并传递给那个execute函数。请查看以下链接:

MySqlDb throws Operand should contain 1 column(s) on insert ignore statement

相关问题 更多 >

    热门问题