简体   繁体   English

使用Python将CSV数据导入postgreSQL

[英]importing CSV data into postgreSQL using Python

I am trying to import CSV data I have into postgreSQL using Python. 我正在尝试使用Python将我拥有的CSV数据导入到postgreSQL中。 It shows an error near (User) when I run the code. 它在我运行代码时显示(User)附近的错误。 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),
                         ^

Try change User to something else and see if the issue is fixed. 尝试将用户更改为其他内容并查看问题是否已修复。 If not, then try to run the example given by PostgreSQL Python guide: http://www.postgresqltutorial.com/postgresql-python/create-tables/ 如果没有,那么尝试运行PostgreSQL Python指南给出的示例: http//www.postgresqltutorial.com/postgresql-python/create-tables/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM