Python cx_Oracle 插入带毫秒的时间戳
Python cx_Oracle Insert Timestamp With Milliseconds
我已阅读帖子:
Insert TIMESTAMP with milliseconds in Oracle with cx_Oracle and Python 3.6 和
, 但其中 none 解决了我的问题。
因为我的Oracletable的列号不止一个
我已经测试了以上两个链接(感谢他们)。然而,它们的效率是有限的。例如,如果 Oracle table 上的时间戳列的列顺序不是 1,则设置 cursor.setinputsizes(cx_Oracle.TIMESTAMP) 不起作用。
根据 Panagiotis Kanavos():
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()
另一方面,我要插入多条记录,比如 ~250k。所以,我需要获取我所有的数据并将它们转换成字典,最后将该字典对象附加到列表中,以便在 executemany 函数中使用输入对象。
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)
传送 25 万条记录效率不高。还有其他的吗solution/s?
编辑:我想在不进行字典转换的情况下插入我的输出数据 (outputsql)。可能吗;如果是那么怎么办?
提前致谢!
Ps: outputsql 是另一个数据库会话查询的输出,它 returns 以正确的毫秒精度记录。
cursorsql = connsql.cursor()
cursorsql.execute('select top 1 record_id, instance_id, record_id from dbo.recordlog (nolock)')
outputsql = cursorsql.fetchall()
无需将您的数据转换为字典。 cursor.setinputsizes()
的 documentation 表明您可以传递位置参数或关键字参数。所以,如果你需要指明第三列应该是一个时间戳,你可以这样做:
cursor.setinputsizes(None, None, cx_Oracle.TIMESTAMP)
这是说前两个绑定位置可以使用默认绑定。这将通过检查调用 cursor.execute()
或 cursor.executemany()
时提供的数据来确定。第三个绑定位置将绑定为时间戳列。
我希望这已经够清楚了!
我已阅读帖子:
Insert TIMESTAMP with milliseconds in Oracle with cx_Oracle and Python 3.6 和
因为我的Oracletable的列号不止一个
我已经测试了以上两个链接(感谢他们)。然而,它们的效率是有限的。例如,如果 Oracle table 上的时间戳列的列顺序不是 1,则设置 cursor.setinputsizes(cx_Oracle.TIMESTAMP) 不起作用。
根据 Panagiotis Kanavos(
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()
另一方面,我要插入多条记录,比如 ~250k。所以,我需要获取我所有的数据并将它们转换成字典,最后将该字典对象附加到列表中,以便在 executemany 函数中使用输入对象。
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)
传送 25 万条记录效率不高。还有其他的吗solution/s?
编辑:我想在不进行字典转换的情况下插入我的输出数据 (outputsql)。可能吗;如果是那么怎么办?
提前致谢!
Ps: outputsql 是另一个数据库会话查询的输出,它 returns 以正确的毫秒精度记录。
cursorsql = connsql.cursor()
cursorsql.execute('select top 1 record_id, instance_id, record_id from dbo.recordlog (nolock)')
outputsql = cursorsql.fetchall()
无需将您的数据转换为字典。 cursor.setinputsizes()
的 documentation 表明您可以传递位置参数或关键字参数。所以,如果你需要指明第三列应该是一个时间戳,你可以这样做:
cursor.setinputsizes(None, None, cx_Oracle.TIMESTAMP)
这是说前两个绑定位置可以使用默认绑定。这将通过检查调用 cursor.execute()
或 cursor.executemany()
时提供的数据来确定。第三个绑定位置将绑定为时间戳列。
我希望这已经够清楚了!