在Oracle中使用cx_Oracle和Python 3.6在毫秒中插入TIMESTAMP

时间:2018-09-03 07:36:26

标签: python sql oracle cx-oracle

我需要在使用Python 3.6和cx_Oracle 6.4.1的Oracle表中插入以毫秒为单位的时间戳。插入操作没问题,但是我在途中损失了毫秒。

时间戳作为日期时间对象传递。有什么想法可以保持毫秒数而不在SQL本身中创建to_timestamp()吗?我想继续在SQL事务中传递日期时间。我使用executemany()的想法是一次插入多个记录。

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()

enter image description here

enter image description here

0 个答案:

没有答案
相关问题