gpt4 book ai didi

python - 使用django将CSV文件导入postgres

转载 作者:行者123 更新时间:2023-11-29 12:04:36 24 4
gpt4 key购买 nike

我正在尝试使用 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 直接导入该文件。请问任何人都可以告诉我做错了什么吗?

最佳答案

我通过逐行插入得到临时解决方案:

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"

关于python - 使用django将CSV文件导入postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33609343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com