pyodbc 执行命令不接受?参数正确吗? [英] pyodbc execute command not accepting ? parameters correctly?

查看:48
本文介绍了pyodbc 执行命令不接受?参数正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码:

cursor.execute('select RLAMBD from ?', OPTable)
print cursor.fetchone().RLAMBD

产生这个错误:

ProgrammingError: ('42S02', '[42S02] [Oracle][ODBC][Ora]ORA-00903: invalid table name\n (903) (SQLExecDirectW)')

OPTable 是一个字母数字字符串,它是我从另一个数据库查询中构建的,其中包含我要从中选择的表名.

OPTable is an alphanumeric string which I've built from another database query which contains the table name I want to select from.

以下代码在同一个脚本中工作得很好.

The following code works just fine within the same script.

sql = 'select RLAMBD from ' + OPTable
cursor.execute(sql)
print cursor.fetchone().RLAMBD

我想以这种方式构建 sql 语句没什么大不了的,但我只是不明白为什么它不接受 ?参数.我什至在同一个脚本中有另一个查询,它使用 ?参数化和工作得很好.不过,工作查询的参数是使用 raw_input 函数生成的.这两个字符串的格式化方式之间是否存在一些细微差别,这会阻止我使查询正常工作?谢谢你们.

I guess it's not a huge deal to build the sql statements this way, but I just don't understand why it's not accepting the ? parameters. I even have another query in the same script which uses the ? parameterization and works just fine. The parameters for the working query are produced using the raw_input function, though. Is there some subtle difference between the way those two strings might be formatted that's preventing me from getting the query to work? Thank you all.

我正在运行 python 2.7 和 pyodbc 3.0.10.

I'm running python 2.7 and pyodbc 3.0.10.

推荐答案

参数占位符不能用于表示对象名称(例如,表或列名称)或 SQL 关键字.它们仅用于传递数据,例如数字、字符串、日期等.

Parameter placeholders cannot be used to represent object names (e.g., table or column names) or SQL keywords. They are only used to pass data values, e.g., numbers, strings, dates, etc..

这篇关于pyodbc 执行命令不接受?参数正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆