Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. #!/usr/bin/python
  2. import MySQLdb
  3. import getpass
  4.  
  5. #f = open('orgchanges.txt', 'r')
  6.  
  7. connection=MySQLdb.connect("localhost","amstan",getpass.getpass("enter the db password: "),"contest")
  8. cursor=connection.cursor()
  9. def query(*args):
  10.     print args
  11.     cursor.execute(*args)
  12.  
  13. def AddUpdateOrg(name):
  14.     #find id org by name
  15.     #if not exist insert and get id
  16.     #if exist return id
  17.     query("""select org_id from organizations where name = %s;""",(name,))
  18.     try:
  19.         return int(cursor.fetchone()[0])
  20.     except TypeError:
  21.         query("""insert into organizations (name) VALUES (%s);""",(name,))
  22.         return AddUpdateOrg(name)
  23.  
  24.  
  25. def PlayerLookup(name):
  26.     #find player by name
  27.     #return None if you can't
  28.     query("""select user_id from users where username = %s;""",(name,))
  29.     result=cursor.fetchone()
  30.     try:
  31.         return int(cursor.fetchone()[0])
  32.     except TypeError:
  33.         return None
  34.    
  35. def UpdatePlayerOrg(id,orgId):
  36.     #update statement by playerid and org id
  37.     print("attempting to update", id, orgId)
  38.     query("update users SET org_id = %s WHERE user_id = %s;",(orgId,id))
  39.  
  40. print "Test"
  41. print "Should be 2: ", AddUpdateOrg("AGH UST")
  42.  
  43. for line in f:
  44.     items = line.strip().split('|')
  45.     org = items[0]
  46.     ids = []
  47.     if len(items) > 1:
  48.         ids = items[1].split(',')
  49.     orgId = AddUpdateOrg(org)
  50.    
  51.     for id in ids:
  52.         if not id.isdigit():
  53.             id = PlayerLookup(id)
  54.         if id != None:
  55.             UpdatePlayerOrg(id,orgId)
  56.         else:
  57.             print("player", id, "not found")
  58.  
  59. connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement