Skip to content
Advertisement

Inserting csv file into a database using Python

In Python I’ve connected to a Postgres database using the following code:

JavaScript

I have created a table called departments and want to insert data into the database from a CSV file. I read the csv in as follows:

JavaScript

And I am trying to insert this data into the table with the following code:

JavaScript

which I’ve seen done in various articles but I keep getting the error:

JavaScript

How can I correct this, or is there another way to insert the csv?

Advertisement

Answer

You have to pass the row information as a tuple. Try this instead:

JavaScript

See the docs for more info: https://www.psycopg.org/docs/usage.html

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement