将数据从pydev导入postgresqll

时间:2017-11-21 17:32:38

标签: python sql eclipse postgresql pydev

我正在尝试使用pgadmin4将数据从eclipse上的pydev移动到postgresql。为什么我的代码打印“Error%s%e”?在postgres中,testtest123表已经创建,但数据没有在那里上传。非常感谢!

#!/usr/bin/python
# -*- coding: utf-8 -*-

import psycopg2
import sys
import csv
from itertools import count
path = r'C:\Users\sammy\Downloads\E0.csv'
with open(path, "r") as csvfile:
    readCSV = csv.reader(csvfile, delimiter=",")
    for row in readCSV:
            new_data = [ row[19]]
            print (new_data)

con = None

try:
    con = psycopg2.connect("host='localhost' dbname='football' user='postgres' password='XXX'")   
    cur = con.cursor()
    cur.execute("CREATE TABLE testtest123 (HY INTEGER PRIMARY KEY)")
    cur.execute("INSERT INTO testtest123(new_data)")
    cur.execute("SELECT * FROM testtest123;")
    con.commit()
except psycopg2.DatabaseError as e:
    if con:
        con.rollback() 

    print ("Error %s % e")
    sys.exit(1)

finally:   
    if con:
        con.close()

print(" ".join(row))
out=open("new_data.csv", "w")
output = csv.writer(out)

for row in new_data:
    output.writerow(row)

out.close()

1 个答案:

答案 0 :(得分:0)

如果表 activerecord: errors: models: user: attributes: some_property: inclusion: 'must be one of [foo, bar]' 已经存在,postgres将不再创建它。
不要在一个testtest123块中包装多个语句 - 这使您很难确定识别错误。

出于调试目的,您可以:

try/except