PythonLearner PythonLearner - 1 month ago 17
Python Question

importing CSV data into postgreSQL using Python

I am trying to import CSV data I have into postgreSQL using Python. It shows an error near (User) when I run the code. Can someone please provide assistance. I am new to Programming so please pardon my stupidity.

import psycopg2
import csv

csv_data = csv.reader(file('SampleData2.csv'))

database = psycopg2.connect (database = "***", user="***", password="***", host="localhost", port="5432")

cursor = database.cursor()
delete = """Drop table if exists Real.SampleDataTwo"""
print (delete)

mydata = cursor.execute(delete)

cursor.execute("""Create Table Real.SampleDataTwo
(User varchar(55),
LastUpdate timestamp,
Week date,
Builder varchar(55),
Traffic integer
);""")

print "Table created successfully"

for row in csv_data:

cursor.execute("INSERT INTO Real.SampleDataTwo (User, LastUpdate, Week, Builder, Traffic)"\
"VALUES (%s,%s,%s,%s,%s)",
row)


cursor.close()
database.commit()
database.close()

print "CSV data imported"


The error it shows is:

Drop table if exists Real.SampleDataTwo

Traceback (most recent call last):
File "C:/Users/Programming/Data.py", line 20, in <module>
);""")
ProgrammingError: syntax error at or near "User"
LINE 2: (User varchar(55),
^

mkj mkj
Answer