You need to enable JavaScript to run this app. Pandas读取Clickhouse数据报Unsupported object错误如何解决?
优惠活动
大模型
产品
解决方案
定价
更多

Pandas读取Clickhouse数据报Unsupported object错误如何解决?

报错原因

你用的MySQL驱动支持直接将Python列表作为IN查询的命名参数传入,会自动解析为符合SQL语法的逗号分隔值,但Clickhouse的Python连接驱动(包括sqlalchemy封装的CK驱动)默认不支持直接传入列表类型的参数,因此会抛出不支持该对象类型的异常。

解决方案

下面给出3种可落地的解决方式,可根据你的使用场景选择:

  • 方案1:将列表转为元组传入,适配CK驱动的参数解析逻辑
    大多数场景下仅需修改参数类型即可生效,无需调整SQL写法:
    userID = [1,2,3,4]
    df = pd.read_sql_query(
    "select id, amount, ds from t_order where id in %(userID)s", 
    ck_engine, 
    params={'userID': tuple(userID)}
    )
    
  • 方案2:手动拼接IN子句参数
    仅适用于参数为数字、可控不会产生SQL注入的场景,实现最简单:
    userID = [1,2,3,4]
    id_str = ','.join(map(str, userID))
    sql = f"select id, amount, ds from t_order where id in ({id_str})"
    df = pd.read_sql_query(sql, ck_engine)
    
  • 方案3:使用Clickhouse原生驱动执行查询
    原生的clickhouse-driver本身支持列表类型的参数传入,查询完成后再转为DataFrame即可:
    from clickhouse_driver import Client
    import pandas as pd
    
    # 初始化CK连接客户端
    client = Client(
        host='你的CK服务地址',
        port=9000,
        user='用户名',
        password='密码',
        database='数据库名'
    )
    userID = [1,2,3,4]
    # 执行查询,原生支持列表参数
    res = client.execute(
        "select id, amount, ds from t_order where id in %(userID)s",
        params={'userID': userID}
    )
    # 结果转为DataFrame
    df = pd.DataFrame(res, columns=['id', 'amount', 'ds'])
    

内容的提问来源于stack exchange,提问作者Scott_Zeng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 17:03:01