【问题标题】:cx_Oracle.DatabaseError: ORA-01841: (full) year must be between -4713 and +9999, and not be 0cx_Oracle.DatabaseError: ORA-01841: (full) year 必须在 -4713 和 +9999 之间,而不是 0
【发布时间】:2020-04-22 10:52:28
【问题描述】:

我有如下 Cx_Oracle 插入查询,

cur.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYYMMDD' ")

cur.execute("INSERT INTO table(col A,col B,col C,col D) values (:1,:2,:3,:4)",(lines['col A'],lines['col B'],lines['col C'],lines['col D']))

这里我的 B&C 列是 Date 数据类型,当我在两者中都有一个值时,它们会成功加载。

但是当我有它有“NULL”或“”时,它会抛出以下错误。

> cx_Oracle.DatabaseError: ORA-01841: (full) year must be between -4713 and +9999, and not be 0

【问题讨论】:

  • 1.列 B 和 C 是否可以为 Nullable ? 2. '' 绝对不是日期
  • 1.是的,它们可以为空。 2.尝试通过传递“NULL”和空值“”。两者都出现相同的错误。

标签: sql python-3.x cx-oracle


【解决方案1】:

在 Python 中必须将空值作为值 None 传递。您不能使用字符串值“NULL”。您也可以经常使用空字符串,Oracle 也将其视为 null,但如果可能的话,我会使用 None

如果您在 SQL*Plus 中执行以下操作,您可以看到这一点。

alter session set nls_date_format = 'YYYYMMDD';
select to_date('') from dual;     /* works -- returns null */
select to_date('NULL') from dual; /* fails with the error you received */

您的示例中的所有代码都不存在。展示一组完整的代码来演示问题会很有帮助,包括用于创建表的 SQL ——假设上面给出的提示是不充分的!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多