i have downloaded a csv file for testing purposes and would like to upload all of the data to postgresql Database. However, i need to have an autoincrementing ID as the first column of the DB. Initially, i created the DB with SQL Query:
CREATE TABLE pps3
( id integer NOT NULL DEFAULT
nextval('products_product_id_seq'::regclass),
"brandname" character varying(25),
"type1" integer,
"type2" integer,
"type3" integer,
"Total" integer )
CSV data:
"brandname","type1","type2","type3","Total"
"brand1","0","0","32","32"
"brand1","0","12","0","12"
I tried to move the data from the CSV with this code:
import csv
import psycopg2
conn = psycopg2.connect("host=localhost dbname=my_django_db user=postgres")
cur = conn.cursor()
with open('PPS-Sep.csv', 'r') as f:
reader = csv.reader(f)
next(reader) # Skip the header row.
for row in reader:
cur.execute(
"INSERT INTO pps3 VALUES (%s, %s, %s, %s,%s)",row)
conn.commit()
This is working fine if I do not create the initial ID column.
However, if I run it like that I get an error message that I am trying to insert the brandname to the ID.
Any ideas on how to go around this?
cur.execute( "INSERT INTO pps3 (brandname, type1, type2, type3, total) VALUES (%s, %s, %s, %s,%s)",row)