Home > OS >  How to Change the Micro sec to Miili sec and adding back to date time object
How to Change the Micro sec to Miili sec and adding back to date time object

Time:01-09

I am trying to collect the data using python file and storing the data in oracle db using the cx_oracle. One column among is ins_timestamp, we are initializing this with ins_timestamp = datetime.datetime.now()and storing in the DB.

The datetime.datetime.now() contains the micro sec, we want the milli sec of datetime.datetime.now() to be stored in the DB.

Can someone help me how to change the micro sec to milli sec in datetime object?

Python 3.8 Thanks in advance

CodePudding user response:

Datetime has isoformat:

import datetime as dt
n= dt.datetime.utcnow()
print(n)
print(n.isoformat(sep=' ', timespec='milliseconds'))

Out:

2023-01-05 22:55:31.693266
2023-01-05 22:55:31.693

CodePudding user response:

With the schema:

drop table t purge;
create table t (c timestamp);

Try code like:

n = datetime.datetime.utcnow()
print(n.isoformat(sep=' ', timespec='milliseconds'))
cursor.setinputsizes(bv=oracledb.DB_TYPE_TIMESTAMP)
cursor.execute("insert into t (c) values (:bv)", bv=n)

Note I am using python-oracledb, which is the latest version of cx_Oracle, see the release announcement. However the code will be similar for cx_Oracle.

  • Related