Insert TIMESTAMP with milliseconds in Oracle with cx_Oracle and Python 3.6

烂漫一生 提交于 2021-02-19 17:47:53

问题


I need to insert a timestamp with milliseconds in an Oracle table with Python 3.6 and cx_Oracle 6.4.1. The insert is done okay but I am losing the milliseconds on the way.

The timestamp is passed as a datetime object. Any ideas how I could keep the milliseconds without making a to_timestamp() in the SQL itself? I would like to keep passing a datetime in the SQL transaction. I use executemany() as the idea is to insert multiple records at once.

import cx_Oracle
import datetime

conf = {
    'username': 'USERNAME',
    'password': 'PASSWORD',
    'tns': 'TNS'
}

d = datetime.datetime.now()
print(d)

db_conn = cx_Oracle.connect(conf['username'], conf['password'], conf['tns'], encoding='UTF-8', nencoding='UTF-8')
cursor = db_conn.cursor()

sql_insert = "INSERT INTO {} VALUES {}".format('timestamp_test', '(:1)')
cursor.prepare(sql_insert)
cursor.executemany(None, [[d]])
db_conn.commit()

来源:https://stackoverflow.com/questions/52145119/insert-timestamp-with-milliseconds-in-oracle-with-cx-oracle-and-python-3-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!