Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import csv # for reading and manipulating the CSV file
  2. import MySQLdb #Mysql Connector
  3.  
  4. #Establish connection to MysqlDB
  5. mysql_db = MySQLdb.connect(host="", user="root", password="", db="edxapp", port=3306)
  6.  
  7. #create a cursor for sql statments
  8. mysql_cursor = mysql_db.cursor()
  9.  
  10. # container for users emails, their countries and their foriegn key id
  11. users_countries_ids = []
  12.  
  13. # loop though CSV file and get the id of each email address from auth_user
  14. input_file = open('country.csv')
  15. csv_f = csv.reader(input_file)
  16. cursor = 0
  17.  
  18. for row in csv_f:
  19. # add all emails and countries code to containes
  20. users_countries_ids.append(row)
  21. # query the id of the email address
  22. mysql_cursor.execute('''select id from auth_user where email="%s"''' % (users_countries_ids[cursor][0]))
  23. user_id = mysql_cursor.fetchone()
  24. user_id = int(user_id[0])
  25. #user_id = int(user_id)
  26. # append id of each email to the container
  27. users_countries_ids[cursor].append(user_id)
  28. # update the country field in auth_userprofile
  29. mysql_cursor.execute('''update auth_userprofile set country=%s where user_id=%s''',(users_countries_ids[cursor][1],users_countries_ids[cursor][2]))
  30. mysql_db.commit()
  31. cursor += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement