2017-11-21 4 views
1

Ich versuche, Daten von Pydev auf Eclipse zu Postgresql mit pgadmin4 zu verschieben. Warum druckt mein Code "Error% s% e"? In postgres wird die Tabelle testtest123 erstellt, aber die Daten werden dort nicht hochgeladen. Danke vielmals!Daten von Pydev in Postgresqll importieren

#!/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() 

Antwort

0

Bei Tabelle existiert testtest123 bereits, Postgres, es wird nicht wieder schaffen.
Nicht mehrere Anweisungen in einem try/except Block umbrechen - dies macht es Ihnen schwer zu bestimmen, um den Fehler zu identifizieren.

Für Debuggen Sie konnte dies:

import traceback 

# ... your code ... 

con = psycopg2.connect("host='localhost' dbname='football' user='postgres' password='XXX'") 
cur = con.cursor() 

try: 
    cur.execute("CREATE TABLE testtest123 (HY INTEGER PRIMARY KEY)") 
    cur.execute("INSERT INTO testtest123(new_data)") 
    cur.execute("SELECT * FROM testtest123;") 
    con.commit() 
except: 
    print ("Error:") 
    traceback.print_exc() 
    con.rollback() 
    sys.exit(1) 
finally: 
    if con: 
     con.close() 
Verwandte Themen