有客自远方来52 2024-05-09 00:06 采纳率: 15.3%
浏览 1
已结题

如何解决未定义列的问题?(语言-python)

以下是我的代码,执行过程中弹出了未定义列的问题,经过排除后,我发现问题出在最后一行代码上“if cea.GID……”,当把它删除后程序正常运行,我该如何解决

def list(cea: Cea):
    """
    根据条件查询图书信息
    :param Hbook: 查询参数
    :return:
    """
    con = None
    try:
        con = dbUtil.getCon()
        cursor = con.cursor()
        # 查询数据
        sql = "SELECT c.Cuser,c.Cname,c.Cage,c.Csex,c.Cphone,c.Caddress,c.Caddtime,g.Gname FROM Crew AS c,Groups AS g " \
              "WHERE c.GID=g.GID"
        if cea != None:
            if cea.Cname.strip() != '':
                sql += " and c.Cname like '%" + cea.Cname + "%' "
            if cea.Cuser.strip() != '':
                sql += " and c.Cuser like '%" + cea.Cuser + "%' "
            if cea.GID != -1:
                sql += " and c.GID = " + str(cea.GID)

        cursor.execute(sql)
        con.commit()
        return cursor.fetchall()
    except Exception as e:
        print(f"123查询数据时发生错误: {e}")
        if con is not None:
            con.rollback()
        return None
    finally:
        dbUtil.closeCon(con)

123查询数据时发生错误: (207, b"Invalid column name 'None'.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n")

  • 写回答

2条回答 默认 最新

  • 专家-皆知 2024-05-09 00:06
    关注

    引自免费微信小程序:皆我百晓生

    请确保cea对象中存在名为GID的列,或者在访问之前使用get()方法处理可能的None值。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月11日
  • 创建了问题 5月9日