Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. import csv, os, psycopg2
  2. from psycopg2.extras import DictCursor
  3.  
  4. TABLE = "community.other_tix_platform_attendees"
  5.  
  6. def db_connect():
  7. db_name = os.environ['db_name']
  8. db_user = os.environ['db_user']
  9. db_host = os.environ['db_host']
  10. db_credentials = os.environ['db_credentials']
  11.  
  12. conn_string = "dbname='" + str(db_name) + "' user='" + str(db_user) + "' host='" + str(db_host) + "' password='" + str(db_credentials) + "'"
  13.  
  14. try:
  15. conn = psycopg2.connect(str(conn_string))
  16. conn.autocommit = True
  17. except:
  18. print "Unable to connect to the database"
  19.  
  20. cur = conn.cursor(cursor_factory=DictCursor)
  21. return cur
  22.  
  23. cur = db_connect()
  24.  
  25. cur.execute('ALTER TABLE %s ADD UNIQUE (email, swoop_id);'%TABLE)
  26.  
  27. with open('usethisone.csv') as csv_file:
  28. csv_reader = csv.reader(csv_file, delimiter=',')
  29. line_count = 0
  30. for row in csv_reader:
  31. if line_count == 0:
  32. line_count += 1
  33. else:
  34. values = (row[4], row[1], ' '.join([row[2],row[3]]), row[3], 'manually added by twood for diego')
  35. cur.execute("""INSERT INTO community.other_tix_platform_attendees
  36. (swoop_id, email, attendee_name, last_name, other_data)
  37. ON CONFLICT DO NOTHING""", values)
  38. line_count +=1
  39. print('WROTE %i lines'%line_count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement