Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def csvwritein(doc):# doc is a django model contains the information of the uploading file
- doc = doc
- conn = psycopg2.connect("dbname='apidb' user='api' host='localhost' password='eric40502' port='5432'")
- readcur = conn.cursor()
- readcur.execute("select exists(select * from information_schema.tables where table_name='%s')" % doc.tablename) # check if same relation is already in database
- check = readcur.fetchone()[0]# check is used to check if same relation exists
- print(check)
- try:# change encoding type according to file type
- fr = open(doc.path,encoding = 'utf-8-sig')
- dr(fr,doc,check)# pass writing database part to another func
- fr.close()
- except Exception as e:
- fr = open(doc.path,encoding = 'big5')
- dr(fr,doc,check)# pass writing database part to another func
- fr.close()
- conn.commit()
- def dr(fr,doc,check): # make datareader as function to keep code 'dry'
- csvt = 0 # check if writing action is first time
- row_id = 1 # used for following id field
- conn = psycopg2.connect("dbname='apidb' user='api' host='localhost' password='eric40502' port='5432'")
- maincur = conn.cursor()
- writecur = conn.cursor()
- datareader = csv.reader(fr, delimiter=',')
- for row in datareader:
- if csvt == 0: # first time in loop(create field) and check no same file exists
- print(doc.tablename)
- if check == True:
- checktrue(doc)
- else:
- tablename = '"%s"' % doc.tablename
- maincur.execute("CREATE TABLE %s (id SERIAL PRIMARY KEY);" % tablename)
- row_count = sum(1 for line in datareader)
- col_count = len(row)
- frow = row
- for i in range(0,col_count,1):
- row[i] = '"%s"' % row[i] # change number to string
- maincur.execute("ALTER TABLE %s ADD %s CITEXT;" % (tablename,row[i]$
- csvt = csvt+1
- fr.seek(0)
- next(datareader)
- elif csvt > 0: # not first time(insert data) and check no same file exists
- for j in range(0,col_count,1):
- if j == 0:
- writecur.execute("INSERT INTO %s (%s) VALUES ('%s');" % (ta$
- else:
- writecur.execute("UPDATE %s SET %s = '%s' WHERE id = '%d';"$
- csvt = csvt+1
- row_id = row_id+1
- else:
- break
- conn.commit()
- maincur.close()
- writecur.close()
- conn.close()
- csvt = 0
- doc = Document.objects.all()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement