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.
Because, column number of my Oracle table is more than one.
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.
According to Panagiotis Kanavos(sqlserver datetime to oracle timestamp problems):
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. 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. Is there any other solution/s?
Edit: I want to insert my output data(outputsql) without dictionary conversion. 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.
cursorsql = connsql.cursor()
cursorsql.execute('select top 1 record_id, instance_id, record_id from dbo.recordlog (nolock)')
outputsql = cursorsql.fetchall()
cursor.setinputsizes(cx_Oracle.TIMESTAMP)works when you have one column, but later you show how to use it with multiple columns. Other comments: don't useprepare(). Instead just pass the SQL statement toexecutemany(). Review the cx_Oracle documentation on usingexecutemany()cx-oracle.readthedocs.io/en/latest/user_guide/… - Christopher Jones