正如主题,这是代码,没有错误消息,但数据没有插入。
这是我的代码,谁能告诉我它有什么问题吗?
import psycopg2
import sys
import os
import glob
import csv
#open the csv folder
dictfile='******'
os.chdir(dictfile)
total=[]
for file in glob.glob("*.csv"):
total.append(file)
con = None
try:
con = psycopg2.connect(host='localhost',database='*****',user='postgres', password='*****')
cur = con.cursor()
for i in range(0,1):
filename='/Users/Shared'+'/'+total[0]
print filename
#better move all files into shared folder
x="copy public.crossref_sample from "+ "'"+filename+"'"+" DELIMITERS ',' CSV"
print x
cur.execute(x)
except psycopg2.DatabaseError, e:
print 'Error %s' % e
sys.exit(1)
finally:
if con:
con.close()
正如 @a_horse_with_no_name 所暗示的,您将关闭 PostgreSQL 数据库连接,但您不会首先提交事务。
如果还没有打开交易,psycopg2 会为您打开一笔交易。它希望您在完成工作后提交此事务。
除非你明确地提交事务,关闭连接将rollback任何已完成的工作。
Try con.commit()
在最后一个复制命令之后。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)