将 Python 日期时间插入到类型为 DATE 的 Oracle 列中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23612300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert Python datetime to Oracle column of type DATE
提问by Alok Agarwal
I am trying to store python datetime object to ORACLE column of type date.
我正在尝试将 python datetime 对象存储到日期类型的 ORACLE 列。
so far, I have used,
到目前为止,我已经使用,
rpt_time = time.strftime('%Y-%m-%d %H:%M:%S') or
rpt_time = str(datetime.datetime.now())
but all are giving ORA-01843: not a valid montn
但所有人都在给 ORA-01843: not a valid montn
I am really confused how to insert ORACLE date type python datetime object
我真的很困惑如何插入 ORACLE 日期类型 python datetime 对象
回答by Piotr Dobrogost
cx_Oracle supports passing objects of class datetime.datetime
. Because of this when you already have object of this class (for example when calling datetime.datetime.now()
) you should not try to format it and pass as a string but instead pass it directly. This way you prevent all errors caused by wrong format of date and/or time.
cx_Oracle 支持传递类的对象datetime.datetime
。因此,当您已经拥有此类的对象时(例如在调用 时datetime.datetime.now()
),您不应该尝试格式化它并将其作为字符串传递,而是直接传递它。这样您就可以防止由错误的日期和/或时间格式引起的所有错误。
Example:
例子:
cursor.execute("INSERT INTO SomeTable VALUES(:now)", {'now': datetime.datetime.now()})
Be aware that you have to take additional steps if you want to prevent truncation of fractional part of seconds. For details please read Mastering Oracle+Python, Part 2: Working with Times and Datesarticle by Przemys?aw Piotrowski.
请注意,如果要防止截断秒的小数部分,则必须采取其他步骤。有关详细信息,请阅读Przemys?aw Piotrowski 撰写的掌握 Oracle+Python,第 2 部分:使用时间和日期的文章。
回答by Hyman_of_All_Trades
As far as my search shows, ORACLE can be picky on dates so this might be what you need to do.
据我的搜索显示,ORACLE 可能对日期很挑剔,所以这可能是您需要做的。
Firstly, check the format of date you have. For example, if you have something like, 2010/01/26:11:00:00AM, then you might want to do following on your cursor execute:
首先,检查您拥有的日期格式。例如,如果您有类似 2010/01/26:11:00:00AM 的内容,那么您可能希望对光标执行以下操作:
insert into x
values(99, to_date('2010/01/26:11:00:00AM', 'yyyy/mm/dd:hh:mi:ssam'));
回答by eliatou
You have to convert date from python to oracle by setting nls_date_format in you session
您必须通过在会话中设置 nls_date_format 将日期从 python 转换为 oracle
>>> rpt_time = time.strftime('%Y-%m-%d %H:%M:%S')
>>> rpt_time
'2014-05-12 21:06:40'
Then before inserting into oracle, do the following
然后在插入oracle之前,执行以下操作
cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'")
回答by Alex W.
datetime.now() in python gives you milliseconds and have to get rid of it before sucessfully writing to Oracle.
python 中的 datetime.now() 给你毫秒,在成功写入 Oracle 之前必须摆脱它。
from datetime import datetime
....
cursor.execute("INSERT INTO myTable VALUES(to_date('" + str(datetime.now().replace(microsecond=0)) + "','yyyy-mm-dd hh24:mi:ss'))")
....
回答by Max Narvaez
You need to inport the datetime library to use datetime functions
您需要导入日期时间库才能使用日期时间函数
from datetime import datetime