Skip to content

When using time formatting in SQL with bind variables, cx_Oracle interprets colons in time format as bind variables - How to handle? #666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Runbacktoo opened this issue Feb 22, 2024 · 2 comments
Labels

Comments

@Runbacktoo
Copy link

cx_Oracle version: 8.3.0

When executing an SQL statement that includes time formatting (HH24:MI:SS) and uses bind variables, cx_Oracle mistakenly interprets the colons in the time format as part of the bind variables, leading to errors in SQL execution. How can this issue be addressed to correctly handle time formats in SQL statements without compromising the use of bind variables?

UPDATE TAB
SET START_TIME = TO_DATE(:delay_date, 'YYYY-MM-DD HH24:MI:SS'),
    LAST_EDIT_DATE = SYSDATE,
    LAST_EDIT_BY = :name WHERE ID = :id
params = {'delay_date': '2023-01-01 12:00:00', 'name': 'E12345', 'id': '1001'}
cursor.execute(sql, params)
@anthony-tuininga
Copy link
Member

What error are you getting? I tried this script:

import cx_Oracle as oracledb

conn = oracledb.connect("user/password@host:port/service_name")
cursor = conn.cursor()

params = {'delay_date': '2023-01-01 12:00:00', 'name': 'E12345', 'id': '1001'}
cursor.execute("""
        select
            to_date(:delay_date, 'YYYY-MM-DD HH24:MI:SS'),
            :name,
            :id
        from dual""", params)
for row in cursor:
    print(row)

and did not get any errors. I also tried it with python-oracledb, the replacement for cx_Oracle, and didn't run into any errors there, either. Can you try this script?

@Runbacktoo
Copy link
Author

What error are you getting? I tried this script:

import cx_Oracle as oracledb

conn = oracledb.connect("user/password@host:port/service_name")
cursor = conn.cursor()

params = {'delay_date': '2023-01-01 12:00:00', 'name': 'E12345', 'id': '1001'}
cursor.execute("""
        select
            to_date(:delay_date, 'YYYY-MM-DD HH24:MI:SS'),
            :name,
            :id
        from dual""", params)
for row in cursor:
    print(row)

and did not get any errors. I also tried it with python-oracledb, the replacement for cx_Oracle, and didn't run into any errors there, either. Can you try this script?

I apologize, it might have been due to my spelling mistake. In subsequent usage, I indeed haven't encountered a similar situation again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants