2023-10-05

Error while inserting rows into db expecting string or bytes object

I'm trying to insert a dataframe, derived from a csv, into an Oracle table and am getting the above message.

try:
    dfTuple = [tuple(x) for x in dfOrders.values]

    sqlTxt = 'INSERT INTO SCHEMA.MYTABLE\
                (Field1, Field2, Field3, Field4, Field5)\
                VALUES (:1, :2, :3, :4, :5)'
    
    cursor.executemany(sqlTxt, [x for x in dfTuple])

    rowCount = cursor.rowcount
    print("number of inserted rows =", rowCount)

    connection.commit()

except Exception as err:
    print('Error while inserting rows into db')
    print(err) 
finally:
    if(connection):
       cursor.close()
       connection.close()

I've tried all types of variations of ( { [ with VALUES and none seem to work. In the DB Fields 1-5 are INT, INT, VARCHAR, DATE, VARCHAR... do I need to somehow convert the dataframe (comes through as all objects) into each correct datatype first?

This is what dfTuple looks like, with the original dfOrders.dtypes showing all as an object:

enter image description here



No comments:

Post a Comment