python获取oracle表结构_使用Python和cx-oracle从oracle以正确的格式获取日期字段

I am retrieving a load of data from one oracle database and then inserting it into another non linked database that is structurally the same.

I am doing it by doing:

select * from xxx where id = parameter

and

Select COLUMN_NAME from user_tab_columns where table_name=xxx

then with zip putting them in a dictionary as table_name:Data to build the insert from

Problem is it is returning the date fields as datetime.datetime(99, 12, 31, 0, 0). I need this as 31-dec-2999. How can I get it to return it like this or do I need to create a regex to do this?

I'm new to all this so if my method seems ridiculous feel free to say so and suggest a better method

Many thanks

Adam

解决方案

The cx_Oracle database adapter is giving you datetime.datetime() objects. Use methods on those objects to format them to a string if you require a different output.

The datetime.strftime() method would be best suited for your purposes:

dtobject.strftime('%d-%b-%Y')

Demo:

>>> import datetime

>>> dtobject = datetime.datetime(2999, 12, 31, 0, 0)

>>> dtobject.strftime('%d-%b-%Y')

'31-Dec-2999'

If, however, Oracle is really returning objects with the year set to 99 (not 2999) you need to repair the data:

if dtobject.year < 100:

dtobject = dtobject.replace(year=dtobject.year + 2900)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值