python cx_Oracle Bind非法变量名

标签 python oracle

以下语句中字典中的 bindind 日期有问题吗??

mySQL = 'SELECT day_key FROM timeday WHERE calendar_date =:calendar'
args = {'calendar':'2016/10/16', 'giftcardnbr': '7777083049519090', 'giftcard_amt': '249.8'}

cursor.execute(mySQL,args)

DatabaseError: ORA-01036: 非法变量名称/编号

为什么此语法会返回不同的错误?

cursor.execute('SELECT day_key FROM timeday WHERE calendar_date =:calendar',{'calendar':'2016/10/16'})

DatabaseError: ORA-01861: 文字与格式字符串不匹配

来自掌握 Oracle Python

named_params = {'dept_id':50, 'sal':1000}
query1 = cursor.execute('SELECT * FROM employees WHERE department_id=:dept_id AND salary>:sal', named_params)

工作正常吗??

谢谢

最佳答案

您的第一个查询不起作用,因为 cx_Oracle 试图将 giftcardnbrgiftcard_amt 绑定(bind)到不存在的绑定(bind)变量。

如果我在 Cursor.execute() 中绑定(bind)正确数量的变量调用一切都很好:

>>> import cx_Oracle
>>> DB = cx_Oracle.connect('ben/****@s1/s1')
>>> cur = DB.cursor()
>>> SQL = "select * from dual where 1 = :a"
>>> cur.execute(SQL, {'a' : 1}).fetchall()
[('X',)]

如果我尝试绑定(bind)一个不存在的变量,那么它会失败并出现相同的错误:

>>> cur.execute(SQL, {'a' : 1, 'b' : 2}).fetchall()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number

您的第二次调用失败,因为 '2016/10/16' 不是日期,它是一个字符串(参见我之前的回答 Comparing Dates in Oracle SQL )。

如果我们构造一个日期,然后用它来与一个日期进行比较,一切都很好:

>>> import datetime
>>> my_date = datetime.datetime(year=2016, month=10, day=16)
>>> my_date
datetime.datetime(2016, 10, 16, 0, 0)
>>> SQL = "select * from dual where date '2016-01-01' = :calendar"
>>> cur.execute(SQL, {'calendar' : my_date }).fetchall()
[]

关于python cx_Oracle Bind非法变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40506948/

相关文章:

python - django grappelli、文件浏览器和 Tiny MCE 插入图像对话框

java - ORA-00933 SQL 命令未正确结束,但在 SQL Developer 中正常

oracle - 匿名 pl/sql block 中的声明顺序

Oracle sqlplus 执行不同编码的文件

sql - PL/SQL Developer 中执行过程的问题

python - Python 程序如何加载并读取文件中的特定行?

python - 删除字典、列表、元组中的循环引用

python - 有没有办法测试 SQLAlchemy 连接?

python - 在 Pandas 中使用 Groupby : assign a value from a column conditioned on another column

引用似乎不存在的约束的 Oracle 唯一约束冲突