《错误记录——SQL上传时错误。错误描述pymysql.err.OperationalError: (1241, ‘Operand should contain 1 column(s)‘)

2021/12/25 19:08:42

本文主要是介绍《错误记录——SQL上传时错误。错误描述pymysql.err.OperationalError: (1241, ‘Operand should contain 1 column(s)‘),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

本次错误2021年12月24日出现,出现在爬虫上传本地数据库中。错误同样无大问题。
问题,描述:

Traceback (most recent call last):
  File "E:\python\lib\site-packages\pymysql\connections.py", line 1156, in read
    first_packet = self.connection._read_packet()
  File "E:\python\lib\site-packages\pymysql\connections.py", line 725, in _read_packet
    packet.raise_for_error()
  File "E:\python\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "E:\python\lib\site-packages\pymysql\err.py", line 143, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.OperationalError: (1241, 'Operand should contain 1 column(s)')

出现原因:
在数据与上传值对应 并无问题的情况下,
数据库中不支持Python中列表格式上传,上传值中出现列表时会出现此种错误

列如:

def sjk(id, biaoti, lianjie, shijian, jianjie, leibie, leibie_wangzhan, biaoqian, tupian, beixuan, tupian_xuwei,
        wangzhi_xuwei, shijian_xuwei, redu_xuwei, laiyuan, laiyuantu, laiyuanurl, shijianchuo):
    # try
    # 改变备选图片列表中的逗号值
    conn = pymysql.connect(password="123456", user="root", database="redianzixun", charset="utf8mb4")
    cursor = conn.cursor()
    value = (
        id, biaoti, lianjie, shijian, jianjie, leibie, leibie_wangzhan, biaoqian, tupian, beixuan1, tupian_xuwei,
        wangzhi_xuwei,
        shijian_xuwei, redu_xuwei, laiyuan, laiyuantu, laiyuanurl, shijianchuo)
    panding = """SELECT * from zixun_bendi where FIND_IN_SET(%s,id)"""
    cursor.execute(panding, id)
    result = cursor.fetchall()
    if len(result) == 0:
        sql = """INSERT IGNORE  INTO  zixun_bendi (id,biaoti,lianjie,shijian,jianjie,leibie,leibie_wangzhan,biaoqian,tupian,beixuan,tupian_xuwei,wangzhan_xuwei,shijian_xuwei,redu_xuwei,laiyuan,laiyuantu,laiyuanurl,shijianchuo  ) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"""
        cursor.execute(sql, value)

其中“beixuan”与”leibie_wangzhan”都是列表格式,
解决方法可以是对这两个列表进行处理转化为字符串格式
例如:

    beixuan = ",".join([str(x) for x in beixuan])
    leibie_wangzhan = ",".join([str(x) for x in leibie_wangzhan])


这篇关于《错误记录——SQL上传时错误。错误描述pymysql.err.OperationalError: (1241, ‘Operand should contain 1 column(s)‘)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程