首页 > 解决方案 > Python - cx_Oracle - 返回 ORA-01847:月份中的某天必须介于 1 和该月的最后一天之间

问题描述

我很想使用 python cx_Oracle 模块连接到 Oracle 数据库。当我尝试执行特定查询时,我得到“ORA-01847:一个月中的一天必须在一个月的 1 和最后一天之间”

询问:

select  case when to_date (STOP_DATE, 'dd-mon-yy')='01-JAN-01' then '000000' else  to_char(to_date(STOP_DATE,'dd-mon-yy'),'mmddyy') end STOP_DATE
                        from table1 where SYS_1= 1234 and rownum < 11

相同的查询在 sqlDeveloper 中运行良好。

非常感谢任何帮助。

Python代码:

import cx_Oracle
connection = None
try:
    dsn_tns = cx_Oracle.makedsn("host", "port", service_name="serviceName")
    connection = cx_Oracle.connect(user="user", password="password", dsn=dsn_tns)
    c = connection.cursor()
    sqlStatement = r'''
                            select  case when to_date (STOP_DATE, 'dd-mon-yy')='01-JAN-01' then '000000' else  to_char(to_date(STOP_DATE,'dd-mon-yy'),'mmddyy') end STOP_DATE
                            from table1 where SYS_1= 1234 and rownum < 11

    '''
    c.execute(sqlStatement)
    resultSet = c.fetchall()
    if not len(resultSet) == 0:
            print("query successful!!!!!!!!!!")
            for row in resultSet:
                for items in row:
                   if items == None:
                      items = ""
                print (items)

    if connection:
        print('all executed')
        connection.close()

except Exception as e:
        print(e)
        if connection:
           connection.close()

标签: pythoncx-oracle

解决方案


推荐阅读