简体   繁体   English

python cx_Oracle绑定非法变量名

[英]python cx_Oracle Bind illegal variable name

Trouble with bindind date in dictionary in the following statements?? 在以下语句中字典中的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: illegal variable name/number DatabaseError:ORA-01036:非法的变量名称/编号

Why does this syntax return a different error? 为什么此语法返回不同的错误?

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

DatabaseError: ORA-01861: literal does not match format string DatabaseError:ORA-01861:文字与格式字符串不匹配

From mastering Oracle Python 从掌握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)

works fine?? 工作正常??

Thanks 谢谢

Your first query doesn't work because cx_Oracle is trying to bind giftcardnbr and giftcard_amt to non-existent bind variables. 您的第一个查询不起作用,因为cx_Oracle试图将giftcardnbrgiftcard_amt绑定到不存在的绑定变量。

If I bind the correct number of variables in a Cursor.execute() call everything is fine: 如果我在Cursor.execute()绑定正确数量的变量,则一切正常:

>>> 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',)]

If I try to bind a non-existent variable then it fails with the same error: 如果我尝试绑定不存在的变量,则它将失败,并显示相同的错误:

>>> 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

Your second call fails because '2016/10/16' is not a date, it's a string (see my previous answer Comparing Dates in Oracle SQL ). 您的第二次呼叫失败,因为'2016/10/16'不是日期,而是一个字符串(请参阅我先前的答案“ Oracle SQL中的比较日期” )。

If we construct a date and then use that to compare to a date everything's fine: 如果我们构造一个日期,然后将其用于与日期进行比较,那么一切都很好:

>>> 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()
[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM