0

I'm using sqlalchemy and to_sql to insert my dateframe into my Oracle table. Everything works, but I added a DATE column to the end of the table, and cannot get sqlalchemy to insert that value.

Here is the short code snippet:

x = datetime.datetime.now()
y = x.strftime("%Y-%m-%d %H:%M:%S")
df['date_appended'] = y

df_ora.to_sql('table_name', con, if_exists='append', index=False, dtype={'date_appended': sqlalchemy.DateTime()})

cx_Oracle.DatabaseError) ORA-01861: literal does not match format string
'date_appended': '2020-12-01 08:18:36'

Any suggestions?

1 Answer 1

1

According to the documentation the sqlalchemy.DateTime() type produces datetime.datetime objects and this would suggest to me that you should provide those object when saving:

x = datetime.datetime.now()
df['date_appended'] = x

df_ora.to_sql('table_name', con, if_exists='append', index=False, dtype={'date_appended': sqlalchemy.DateTime()})

Note that this is an educated guess

Sign up to request clarification or add additional context in comments.

4 Comments

That gives me this error: ORA-01830: date format picture ends before converting entire input string. Seems Oracle wants the column type to be TIMESTAMP, not just DATE. I find this curious........
@LandonStatis if you want to use DATE then you probably want the python type datetime.date and sqlalchemy.Date()
So, are you saying x = datetime.date.now() ??
If you want the current date you should use datetime.date.today()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.