Advertisement
Guest User

Untitled

a guest
Nov 27th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. def csvwritein(doc):# doc is a django model contains the information of the uploading file
  2. doc = doc
  3. conn = psycopg2.connect("dbname='apidb' user='api' host='localhost' password='eric40502' port='5432'")
  4. readcur = conn.cursor()
  5. readcur.execute("select exists(select * from information_schema.tables where table_name='%s')" % doc.tablename) # check if same relation is already in database
  6. check = readcur.fetchone()[0]# check is used to check if same relation exists
  7. print(check)
  8. try:# change encoding type according to file type
  9. fr = open(doc.path,encoding = 'utf-8-sig')
  10. dr(fr,doc,check)# pass writing database part to another func
  11. fr.close()
  12. except Exception as e:
  13. fr = open(doc.path,encoding = 'big5')
  14. dr(fr,doc,check)# pass writing database part to another func
  15. fr.close()
  16. conn.commit()
  17.  
  18. def dr(fr,doc,check): # make datareader as function to keep code 'dry'
  19. csvt = 0 # check if writing action is first time
  20. row_id = 1 # used for following id field
  21. conn = psycopg2.connect("dbname='apidb' user='api' host='localhost' password='eric40502' port='5432'")
  22. maincur = conn.cursor()
  23. writecur = conn.cursor()
  24. datareader = csv.reader(fr, delimiter=',')
  25. for row in datareader:
  26. if csvt == 0: # first time in loop(create field) and check no same file exists
  27. print(doc.tablename)
  28. if check == True:
  29. checktrue(doc)
  30. else:
  31. tablename = '"%s"' % doc.tablename
  32. maincur.execute("CREATE TABLE %s (id SERIAL PRIMARY KEY);" % tablename)
  33. row_count = sum(1 for line in datareader)
  34. col_count = len(row)
  35. frow = row
  36. for i in range(0,col_count,1):
  37. row[i] = '"%s"' % row[i] # change number to string
  38. maincur.execute("ALTER TABLE %s ADD %s CITEXT;" % (tablename,row[i]$
  39. csvt = csvt+1
  40. fr.seek(0)
  41. next(datareader)
  42. elif csvt > 0: # not first time(insert data) and check no same file exists
  43. for j in range(0,col_count,1):
  44. if j == 0:
  45. writecur.execute("INSERT INTO %s (%s) VALUES ('%s');" % (ta$
  46. else:
  47. writecur.execute("UPDATE %s SET %s = '%s' WHERE id = '%d';"$
  48. csvt = csvt+1
  49. row_id = row_id+1
  50. else:
  51. break
  52. conn.commit()
  53. maincur.close()
  54. writecur.close()
  55. conn.close()
  56. csvt = 0
  57. doc = Document.objects.all()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement