Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Python datetime to Oracle column of type DATE

I am trying to store python datetime object to ORACLE column of type date.

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

I am really confused how to insert ORACLE date type python datetime object

like image 579
Alok Agarwal Avatar asked May 12 '14 14:05

Alok Agarwal


1 Answers

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.

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 Dates article by Przemysław Piotrowski.

like image 158
Piotr Dobrogost Avatar answered Nov 01 '22 18:11

Piotr Dobrogost