[英]import data from pydev to postgresqll
I am trying to move data from pydev on eclipse to postgresql using pgadmin4. 我正在尝试使用pgadmin4将数据从Eclipse上的pydev迁移到postgresql。 Why is my code printing "Error %s % e"?
为什么我的代码打印“错误%s%e”? In postgres, the testtest123 table is getting created but the data is not being uploaded there.
在postgres中,正在创建testtest123表,但是没有将数据上传到那里。 Many thanks!
非常感谢!
#!/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()
In case of table testtest123
already exists, postgres wont create it again. 如果表
testtest123
已经存在,postgres将不会再次创建它。
Dont wrap multiple statements in one try/except
block - this makes it hard for you to determine to identify the error. 不要将多个语句包装在一个
try/except
块中-这使您难以确定识别错误。
For debugging purposes you could this: 出于调试目的,您可以这样做:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.