【问题标题】:pyodbc - Passing parameterspyodbc - 传递参数
【发布时间】:2020-03-06 10:52:06
【问题描述】:

好的,我尝试了 SO 中所有可能的情况,但都没有成功。

我在调用需要传递 4 个参数的已保存查询时遇到问题(谈论访问数据库),我有一个简单的代码:

params = ()
sql_saldo = "{CALL KUP_BalansKupDosp(204701,0,#3/5/2020#,0)}"
saldo = cursor.execute(sql_saldo)
for row in saldo.fetchall():
    print(row)

这行得通!

但是由于我需要这些参数是动态的,所以当我尝试时:

params = ('204701','0,#3/5/2020#','0')
sql_saldo = "{CALL KUP_BalansKupDosp(?,?,?,?)}"
saldo = cursor.execute(sql_saldo, params)
for row in saldo.fetchall():
    print(row)

我收到一个错误Data type mismatch in criteria expression

这是原始参数定义 PARAMETERS InKupID Long, InSekID Long, InDatum DateTime, InToler Currency = 1; 所以我想它需要字符串、字符串、日期时间和 int。 所以当我尝试:

today_row = date.today()
today = today_row.strftime("%d/%m/%Y")
params = (204701,0,today,0)
sql_saldo = "{CALL KUP_BalansKupDosp(?,?,?,?)}"
saldo = cursor.execute(sql_saldo, params)
for row in saldo.fetchall():
    print(row)

我也遇到了同样的错误,我什至尝试了所有可能的组合,但我就是想不通。

PS。我的连接很好,因为除了这个之外我还有 10 个查询。

有人遇到过类似的问题吗?

【问题讨论】:

    标签: python python-3.x pyodbc pypyodbc python-sql


    【解决方案1】:

    我设法用这个来解决它:

    params = (204701, 0, today, 0)
    sql_saldo = "{CALL KUP_BalansKupDosp(%s,%s,%s,%s)}" % params
    saldo = cursor.execute(sql_saldo)
    for row in saldo.fetchall():
        print(row)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多