简体   繁体   English

python cx_Oracle无法更新空时间戳值

[英]python cx_Oracle fail to update null timestamp value

I'm trying to update TIMESTAMP column in table with None value in python code. 我正在尝试使用python代码中的None值更新表中的TIMESTAMP列。

It worked perfectly when using insert statement with null value. 当使用带有空值的插入语句时,它可以完美地工作。

But when using update statement, it doesn't work!! 但是当使用更新语句时,它不起作用!

following is test code for your understanding. 以下是供您理解的测试代码。

(The reason why I'm updating 'None' value is that the new value is from the other database, and I want to update the value with the new one, and some of the values are NULL.) (之所以更新“ None”值,是因为新值来自另一个数据库,并且我想用新的数据库来更新值,而某些值是NULL。)

:1 is '20160418154000' type string in python code but when it is 'None' value it raise exception. :1是python代码中的'20160418154000'类型字符串,但当其为'None'值时会引发异常。

INSERT INTO TEST_TABLE (ARR_TIME) VALUES(TO_TIMESTAMP(:1, 'YYYYMMDDHH24MISS'))

it works well!! 它很好用!!

UPDATE TEST_TABLE SET ARR_TIME = TO_TIMESTAMP(:1, 'YYYYMMDDHH24MISS')

it doesn't work!! 它不起作用!

error message : ORA-00932: inconsistent datatypes: expected - got NUMBER 错误消息:ORA-00932:数据类型不一致:预期的-得到了NUMBER

I think cx_Oracle recognize the None value in python as number (0??) and it cannot be converted to 'YYYYMMDDHH24MISS' string type. 我认为cx_Oracle将python中的None值识别为数字(0 ??),并且无法将其转换为'YYYYMMDDHH24MISS'字符串类型。

Is there way to update NULL value in TIMESTAMP column? 有没有办法更新TIMESTAMP列中的NULL值?

Yes, there is. 就在这里。 Unless you specify otherwise, nulls are bound as type string. 除非另外指定,否则将空值绑定为字符串类型。 You can override this, though, using the following code: 但是,您可以使用以下代码来覆盖它:

cursor.setinputsizes(cx_Oracle.TIMESTAMP)

See here for documentation: 请参阅此处以获得文档:

http://cx-oracle.readthedocs.org/en/latest/cursor.html#Cursor.setinputsizes http://cx-oracle.readthedocs.org/en/latest/cursor.html#Cursor.setinputsizes

NOTE: you could have also solved this by using this code instead: 注意:您也可以通过使用以下代码解决此问题:

update test_table set arr_time = :1

There is no need to convert the data using to_timestamp() as cx_Oracle can bind timestamp values directly (use datetime.datetime) and if you bind None Oracle will implicitly convert for you. 无需使用to_timestamp()转换数据,因为cx_Oracle可以直接绑定时间戳值(使用datetime.datetime),并且如果您绑定None,Oracle将为您隐式转换。

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

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