You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Teradatasql Python调用存储过程时无法传递列名及命名参数问题

解决方案

teradatasql模块原生仅支持位置参数(即问号?占位符),未内置命名参数传参能力,你可以通过以下两种方式实现需求:

  • 方案1:直接使用位置参数绑定
    你的存储过程有5个IN参数,参数顺序固定为transId、name、add、zip、area,调用时按顺序传递参数列表即可,示例代码如下:
with teradatasql.connect ('{"host":"whomooz","user":"guest","password":"please"}') as con:
    with con.cursor () as cur:
        # 按存储过程参数定义的顺序传值,对应transId、name、add、zip、area
        cur_execute(cur, "{call examplestoredproc (?, ?, ?, ?, ?)}", ["trans123", "张三", "XX街道1号", "100000", "华东"])
  • 方案2:封装命名参数适配逻辑
    如果不想记忆参数顺序,你可以修改自定义的cur_execute函数,加入命名参数到位置参数的映射逻辑,示例如下:
# 和存储过程定义的参数顺序保持一致即可
STORED_PROC_PARAM_ORDER = ["transId", "name", "add", "zip", "area"]

def cur_execute (cur, sSQL, params=None):
    print()
    # 传入字典自动转为符合顺序的参数列表
    if isinstance(params, dict):
        params = [params[key] for key in STORED_PROC_PARAM_ORDER]
    print("cur.execute", sSQL, "bound values", params)
    cur.execute(sSQL, params)

# 调用时直接传字典,无需关心参数顺序
with teradatasql.connect ('{"host":"whomooz","user":"guest","password":"please"}') as con:
    with con.cursor () as cur:
        cur_execute(
            cur, 
            "{call examplestoredproc (?, ?, ?, ?, ?)}", 
            {
                "name": "张三",
                "transId": "trans123",
                "zip": "100000",
                "add": "XX街道1号",
                "area": "华东"
            }
        )

注意不要直接将参数值拼接到SQL语句中,始终使用模块自带的参数绑定能力,避免SQL注入风险。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 23:15:06