2

I am using the following code to copy data from a .csv file into a table:

csv file (comma separated) with header:

uid,name,val1,val2,file
SKU001,glasses,blue,metal,block_0
...

create table:

ih_sql = 'CREATE TABLE "sku_data" (uid VARCHAR, name VARCHAR, val1 VARCHAR, val2 VARCHAR, file VARCHAR, PRIMARY KEY (uid));'
engine.execute(ih_sql)
#<sqlalchemy.engine.result.ResultProxy at 0x7fee9b8ff550>

copy data:

ih_file = os.path.abspath(f'{PATH}data.csv')
copy_sql = f"COPY sku_data FROM '{ih_file}' WITH CSV HEADER DELIMITER ','"
engine.execute(copy_sql)
#<sqlalchemy.engine.result.ResultProxy at 0x7fee9b8ff710>

However the resultant table is empty, zero rows were created. How can I work out why no data was copied from the csv file?

proximacentauri
  • 1,749
  • 5
  • 25
  • 53

1 Answers1

2

You have to explicitly commit the transaction, e.g.:

copy_sql = f"COPY sku_data FROM '{ih_file}' WITH CSV HEADER DELIMITER ','"
connection = engine.connect()
with connection.begin():
    connection.execute(copy_sql)

Read more on Understanding Autocommit.

klin
  • 112,967
  • 15
  • 204
  • 232