I want to insert data with type(bytes) into Oracle table into column raw(16) using Python.
INFO - this is data which I want to insert:
('1', "'test'", "to_date('2021-09-28 15:31:02','YYYY-MM-DD HH24:MI:SS')", b'\xb5V\xad\x04E\xd3V\x1b\x04B\xedL]~W\xf5')
But I have errors
sql = f"INSERT /*+ APPEND */ INTO {table} {target_fields} VALUES ({','.join(values)})"
TypeError: sequence item 3: expected str instance, bytes found
This is my code:
def get_md5(d):
b = pickle.dumps(d)
return hashlib.md5(b).digest()
for row in rows:
i += 1
lst = []
for cell in row:
if isinstance(cell, str):
lst.append("'" + str(cell).replace("'", "''") + "'")
elif cell is None:
lst.append('NULL')
elif isinstance(cell, float) and numpy.isnan(cell): # coerce numpy NaN to NULL
lst.append('NULL')
elif isinstance(cell, numpy.datetime64):
lst.append("'" + str(cell) + "'")
elif isinstance(cell, datetime):
lst.append(
"to_date('" + cell.strftime('%Y-%m-%d %H:%M:%S') + "','YYYY-MM-DD HH24:MI:SS')"
)
else:
lst.append(str(cell))
lst.append("to_date('" + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "','YYYY-MM-DD HH24:MI:SS')")
s = get_md5(lst)
lst.append(s)
values = tuple(lst)
print('values', values)
sql = f"INSERT /*+ APPEND */ INTO {table} {target_fields} VALUES ({','.join(values)})"
print(sql)
cur.execute(sql)
INSERT INTO table_name (col1, col2, col3) VALUES (?, ?, ?)and then use bind variables to pass the values.executemanyrather thanexecute.