简体   繁体   English

Python cx_Oracle 以毫秒为单位插入时间戳

[英]Python cx_Oracle Insert Timestamp With Milliseconds

I have read the posts: Insert TIMESTAMP with milliseconds in Oracle with cx_Oracle and Python 3.6 and sqlserver datetime to oracle timestamp problems , but none of them solved my problem.我已阅读以下帖子: 使用 cx_Oracle 和 Python 3.6sqlserver 日期时间在 Oracle 中以毫秒为单位插入 TIMESTAMP 到 ZA189C633D9995E11BF8607170EC94

Because, column number of my Oracle table is more than one.因为,我的 Oracle 表的列数不止一个。

I have tested both above links(thanks to them).我已经测试了上述两个链接(感谢他们)。 Yet, their efficiency are limited.然而,它们的效率是有限的。 Such as, if the column order of the timestamp column on Oracle table is not 1 then setting cursor.setinputsizes(cx_Oracle.TIMESTAMP) does not work.例如,如果 Oracle 表上的时间戳列的列顺序不是 1,则设置 cursor.setinputsizes(cx_Oracle.TIMESTAMP) 不起作用。

According to Panagiotis Kanavos( sqlserver datetime to oracle timestamp problems ):根据 Panagiotis Kanavos( sqlserver datetime 到 oracle 时间戳问题):

ts = datetime.datetime.now()
cursor.prepare("INSERT INTO python_tstamps VALUES(:t_val)")

cursor.setinputsizes(t_val=cx_Oracle.TIMESTAMP)

cursor.execute(None, {'t_val':ts})
db.commit()

On the other hand, I have multiple records to insert like ~250k.另一方面,我有多个记录要插入,比如 ~250k。 So, I need to fetch all my data and convert to them into dictionary, and finally append that dictionary object to a list in order to use input object in executemany function. So, I need to fetch all my data and convert to them into dictionary, and finally append that dictionary object to a list in order to use input object in executemany function.

column_name = ["instance_id", "record_id", "record_id1"]

dict1 = dict(zip(column_name,outputsql[0]))

for key in dict1:
    print(key)

t = []
t.append(dict1)
cursororacle.prepare("INSERT /*+ append */INTO schematest.test  VALUES (:instance_id, :record_id, :record_id1)")
cursororacle.setinputsizes(instance_id=cx_Oracle.TIMESTAMP, record_id=cx_Oracle.TIMESTAMP)
cursororacle.executemany(None, t)

Conveting 250k records is not efficient.转换 25 万条记录效率不高。 Is there any other solution/s?还有其他解决方案吗?

Edit: I want to insert my output data(outputsql) without dictionary conversion.编辑:我想插入我的 output 数据(outputsql)而不进行字典转换。 Is it possible;可能吗; if it is then how?如果是那怎么办?

Thanks in advance!提前致谢!

Ps: outputsql is output of another db session query and it returns records with correct milliseconds precision. Ps: outputsql 是另一个 db session 查询的 output ,它以正确的毫秒精度返回记录。

cursorsql = connsql.cursor()
cursorsql.execute('select top 1  record_id, instance_id, record_id from dbo.recordlog  (nolock)')
outputsql = cursorsql.fetchall()

There is no need to convert your data to dictionaries.无需将数据转换为字典。 The documentation for cursor.setinputsizes() shows that you can pass either positional arguments or keyword arguments. cursor.setinputsizes()文档显示您可以传递位置 arguments 或关键字 arguments。 So, if you need to indicate that the third column is supposed to be a timestamp, you can do the following:因此,如果您需要指示第三列应该是时间戳,您可以执行以下操作:

cursor.setinputsizes(None, None, cx_Oracle.TIMESTAMP)

What this does is says that the first two binding positions can use the default binding.这样做是说前两个绑定位置可以使用默认绑定。 This will be determined by examining the data that is supplied when cursor.execute() or cursor.executemany() is called.这将通过检查cursor.execute()cursor.executemany()时提供的数据来确定。 The third binding position will be bound as a timestamp column.第三个绑定 position 将绑定为时间戳列。

I hope this is clear enough!我希望这足够清楚!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM