SHARE
TWEET

Untitled

a guest Jun 19th, 2017 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     #TODO
  15.     #find id org by name
  16.     #if not exist insert and get id
  17.     #if exist return id
  18.     query("""select user_id from users where username = %s;""",(name,))
  19.     try:
  20.         return int(cursor.fetchone()[0])
  21.     except TypeError:
  22.         query("""insert into organizations (name) VALUES (%s);""",(name,))
  23.         AddUpdateOrg(name)
  24.  
  25.  
  26. def PlayerLookup(name):
  27.     #find player by name
  28.     #return None if you can't
  29.     query("""select user_id from users where username = %s;""",(name,))
  30.     result=cursor.fetchone()
  31.     try:
  32.         return int(cursor.fetchone()[0])
  33.     except TypeError:
  34.         return None
  35.    
  36. def UpdatePlayerOrg(id,orgId):
  37.     #update statement by playerid and org id
  38.     print("attempting to update", id, orgId)
  39.     query("update users SET org_id = %s WHERE user_id = %s;",(orgId,id))
  40.  
  41. for line in f:
  42.     items = line.strip().split('|')
  43.     org = items[0]
  44.     ids = []
  45.     if len(items) > 1:
  46.         ids = items[1].split(',')
  47.     orgId = AddUpdateOrg(org)
  48.    
  49.     for id in ids:
  50.         if not id.isdigit():
  51.             id = PlayerLookup(id)
  52.         if id != None:
  53.             UpdatePlayerOrg(id,orgId)
  54.         else:
  55.             print("player", id, "not found")
  56.  
  57. connection.close()
RAW Paste Data
Top