to_date()的cx_Oracle字符串表示形式会引发错误

问题描述 投票:0回答:1

我正在基于字典的值构建动态更新语句。

dict1 = {"name": "Test name",
         "id": 100,
         "location": "",
         "custom": "01/01/2020"}

print(dict1)
print()
dict2 = {}
func1 = 'to_date({}, "dd/mm/yyyy")'
for k, v in dict1.items():
    if k == 'custom':
        v = func1.format(f'"{v}"')

    dict2[k] = v

print(dict2)
{'name': 'Test name', 'id': 100, 'location': '', 'custom': 'to_date("01/01/2020", "dd/mm/yyyy")'}

dictionary是按预期的方式构建的,因为func1被定义为字符串。但是,我试图查看是否有一种方法可以将存储在dict中的'to_date(“ 01/01/2020”,“ dd / mm / yyyy”)'更改为to_date(“ 01/01 / 2020“,” dd / mm / yyyy“)不带单引号(不是字符串)

我在使用cx_oracle对oracle进行进一步处理时使用此值。更新语句将引发错误'ORA-01858:在需要数字的位置找到了非数字字符'

update table1 set dt='to_date("01/01/2020", "dd/mm/yyyy")' where x=y

任何建议都会有所帮助。

python sql python-3.x oracle cx-oracle
1个回答
0
投票

to_date()放入SQL语句中,仅绑定数据本身:

dict1 = {"name": "Test name",
         "id": 100,
         "location": "wherever",
         "custom": "01/01/2020"}

sql = """update table1 set dt=to_date(:custom, 'dd/mm/yyyy') where id=:id and location=:location and name = :name"""

cursor.execute(sql, dict1)

请注意绑定变量名称与字典键名称匹配。

关于绑定的cx_Oracle文档为here

© www.soinside.com 2019 - 2024. All rights reserved.