2015-11-09 42 views
1

我正嘗試使用Django將csv文件導入到postgres DB中。使用Django將CSV文件導入到postgres中

我嘗試以下功能:

import os 
from django.db import models 
import psycopg2 
from postgres_copy import CopyMapping 

host = 'localhost' 
port = '5432' 
dbname = 'sellerhub' 
username = 'postgres' 
password = 'postgres' 


class Reports: 
    def __init__(self): 
     global host, port, dbname, username, password 
     try: 
      self.db_conn = psycopg2.connect("host=%s port=%s dbname=%s user=%s password=%s" %(host, port, dbname, username, password)) 
     except psycopg2.OperationalError: 
      print "Database Not Found Or the Credentials are wrong." 
     self.cur = self.db_conn.cursor() 
def saveUploadedInventory(self, inventory_file): 
     #print "Inventory File" 
     with open('uploaded_inventory_sheet.csv','wb+') as destination: 
      for chunk in inventory_file.chunks(): 
       destination.write(chunk) 
     #print "Inventory Saved." 
     copy_sql = """copy fk_invent_temp from stdin WITH CSV HEADER DELIMITER as ',' """ 
     #print "query created" 
     with open('uploaded_inventory_sheet.csv','r') as pmt_file: 
      self.cur.copy_expert(sql=copy_sql, file=pmt_file) 
     #print "file uploades" 
     os.system('rm uploaded_inventory_sheet.csv') 
     #print "removes file" 

setting.py

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.postgresql_psycopg2', 
     'NAME': 'sellerhub', 
     'USER': 'postgres', 
     'PASSWORD': 'postgres', 
     'HOST': 'localhost', 
     'PORT': '5432', 
     } 
    } 

這個功能是沒有錯誤完全地執行,

但在fk_invent表中沒有數據。

如果我使用成功上傳的PGAdmin3 UI直接導入該文件。 請問任何人可以告訴我做錯了什麼?

+0

有太多的信息缺失。這是什麼類的方法?什麼是'self.cur'和'self.cur.copy_expert'? –

+0

cur是從數據庫和複製專家的連接是爲了複製數據庫 –

+0

但是沒有看到copy_expert的代碼,我們怎麼知道它出錯了? –

回答

2

我通過線獲得通過插入線臨時解決方案:

with open('uploaded_inventory_sheet.csv','wb+') as destination: 
      for chunk in inventory_file.chunks(): 
       destination.write(chunk) 
     print "Inventory Saved." 
     reader = csv.reader(open('uploaded_inventory_sheet.csv','rb')) 
     count = 0 
     for row in reader: 
      if count == 0: 
       count=1 
       continue 
      # print row[0],"\n" 
      count = count +1 
      try: 
       self.cur.execute("""INSERT into fk_payment_temp values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""",(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],row[10],row[11],row[12],row[13],row[14],row[15],row[16],row[17],row[18],row[19],row[20],row[21],row[22],row[23],row[24],row[25],row[26],row[27],row[28],row[29],row[30],row[31],row[32],row[33],row[34],row[35],row[36],row[37],row[38],row[39],row[40],row[41],row[42],row[43],row[44])) 
       print "INSERT into fk_payment_temp values('",row[0],"','",row[1],"''",row[2],"','",row[3],"''",row[4],"','",row[5],"''",row[6],"','",row[7],"''",row[8],"','",row[9],"''",row[10],"','",row[11],"''",row[12],"','",row[13],"''",row[14],"','",row[15],"''",row[16],"','",row[17],"''",row[18],"','",row[19],"''",row[20],"','",row[21],"''",row[22],"','",row[23],"''",row[24],"','",row[25],"''",row[26],"','",row[27],"''",row[28],"','",row[29],"''",row[30],"','",row[31],"''",row[32],"','",row[33],"''",row[34],"','",row[35],"''",row[36],"','",row[37],"''",row[38],"','",row[39],"''",row[40],"','",row[41],"''",row[42],"','",row[43],"''",row[44],"','",row[45],"')" 
      except: 
       pass 
     print count 
     self.cur.callproc("flip_payment_test") 
     self.cur 
     self.db_conn.commit() 

     print "file uploades" 
     os.system('rm uploaded_inventory_sheet.csv') 
     print "removes file" 
0

最簡單和最乾淨的解決方案是用Django manage.py命令導入一個json夾具文件。所以,我建議您在CSV文件轉換爲json,並與manage.py loaddata命令加載到數據庫:

python manage.py loaddata yourfile.json 

你可以在互聯網上找到CSV至JSON轉換器。

+0

csv文件由非特定用戶選擇 –