2012-12-13 32 views
2

作爲主題,這是代碼,沒有錯誤消息,但數據沒有得到插入。 這是我的代碼,誰能告訴我它有什麼問題?使用Postgres和Python不能「複製」

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() 
+2

我在代碼中看不到提交 –

+0

謝謝,它現在可以運行 – user1652287

回答

4

正如@a_horse_with_no_name所暗示的那樣,您正在關閉PostgreSQL數據庫連接,但您並未首先提交事務。

psycopg2爲您打開一個交易,如果沒有一個已經打開。它希望您在完成工作後執行此事務。

除非你明確提交事務,關閉連接會回滾一直所做的任何工作。

嘗試con.commit()在最後的複製命令之後。