使用参数执行SQL时出现KeyError

时间:2018-07-25 10:26:12

标签: python teradata

我在运行以下代码时得到KeyError。我正在尝试使用单独的parameters变量传递参数。

代码:

import teradata

host,username,password = 'hostname','uname', 'pwd'
udaExec = teradata.UdaExec (appName="APtest", version="1.0", logConsole=False)
connect = udaExec.connect(method="odbc",system=host, username=username, password=password, dsn="dsnname")

val1='NULL'
val2='NULL'
parameters={'param1':val1, 'param2': val2}

qry="""
    SELECT number
    FROM table
    WHERE number = %(param1)s
    AND col=%(param2)s
    """

connect.execute(qry, parameters)

错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/lib/python2.7/site-packages/teradata/udaexec.py", line 675, in execute
    self.internalCursor.execute(query, params, **kwargs)
  File "/tmp/lib/python2.7/site-packages/teradata/udaexec.py", line 745, in execute
    self._execute(self.cursor.execute, query, params, **kwargs)
  File "/tmp/lib/python2.7/site-packages/teradata/udaexec.py", line 787, in _execute
    logParamCharLimit)
  File "/tmp/lib/python2.7/site-packages/teradata/udaexec.py", line 875, in _getParamsString
    if isinstance(params[0], (list, tuple)):
KeyError: 0

如果我以下面的方式编写查询,那么它可以工作,但是我的参数列表很长,因此需要在单独的参数变量中进行查询。

这有效:

qry="""
    SELECT number
    FROM table
    WHERE number = '%s'
    AND col='%s'
    """ % (val1, val2)

1 个答案:

答案 0 :(得分:0)

显然,teradata不支持参数字典。请改用列表。

find .git/modules/ -name config -exec grep url {} \;