如何从pydev获取独特的数据到postgres

时间:2017-11-22 16:09:03

标签: python sql eclipse postgresql pydev

我有两个代码,它们分开工作,但我不确定如何将它们集成在一起。我想使用pgadmin4使用eclipse到postgresql从pydev获取唯一的裁判名称。非常感谢你的帮助。

代码1:     #!的/ usr / bin中/蟒蛇     # - - 编码: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=",")
    firstline = 1
    con = None
    con = psycopg2.connect("host='localhost' dbname='football' user='postgres' password='XXX'")   
    cur = con.cursor()
    cur.execute("DROP TABLE Referees")
    cur.execute("CREATE TABLE Referees (RefereeID VARCHAR, RefereeName VARCHAR)")

    try:
        for row in readCSV:
            if firstline:
                firstline=0
                continue 
            RefereeID = row[10]
            RefereeName = row[10]
            data = (RefereeID, RefereeName)
            query = "INSERT INTO Referees (RefereeID, RefereeName) VALUES (%s, %s);"
            print (RefereeID)
            print (RefereeName)
            cursor = con.cursor()
            cursor.execute(query, data)
    except psycopg2.DatabaseError as e:
        if con:
            con.rollback() 
            print ("Error %s % e", e)
            sys.exit(1) 
    finally:
        if con:
            con.commit()
            con.close()

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

for row in data:
    output.writerow(row)

out.close()

代码2:     import csv

path = r'C:\Users\sammy\Downloads\E0.csv'
with open(path, "r") as csvfile:
    readCSV = csv.reader(csvfile, delimiter=",")

    refereename= []

    class Teams:
         for row in readCSV:
            referee= row[10]
            refereename.append(referee)

        for x in refereename:
            if x not in uniquerefereename:
                uniquerefereename.append(x)

 print (refereename)
print (uniquerefereename)

 refereesorted= sorted(uniquerefereename)

 print (refereesorted)

0 个答案:

没有答案