加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

使用Python将CSV数据导入postgreSQL

发布时间:2020-12-20 11:55:34 所属栏目:Python 来源:网络整理
导读:我正在尝试使用 Python将我拥有的CSV数据导入到postgreSQL中.它在我运行代码时显示(User)附近的错误.有人可以提供帮助.我是编程新手所以请原谅我的愚蠢. import psycopg2import csvcsv_data = csv.reader(file('SampleData2.csv'))database = psycopg2.conne
我正在尝试使用 Python将我拥有的CSV数据导入到postgreSQL中.它在我运行代码时显示(User)附近的错误.有人可以提供帮助.我是编程新手所以请原谅我的愚蠢.

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)",row)


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

print "CSV data imported"

它显示的错误是:

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),^

解决方法

我认为不允许用户作为列名.见 https://www.postgresql.org/docs/current/static/sql-keywords-appendix.html#KEYWORDS-TABLE

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读