Krenair

Convert ZT DB to SP

Sep 24th, 2013
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. import getpass, MySQLdb, re
  2. conn = MySQLdb.connect(host = raw_input("Hostname: "), port = int(raw_input("Port number: ")), user = raw_input("Username: "), passwd = getpass.getpass(), db = raw_input("DB name: "))
  3. db = conn.cursor()
  4. db.execute("SELECT * FROM zerotolerance")
  5. rowsToInsert = []
  6. steamIdRegex = re.compile("^STEAM_[0-7]:[01]:\\d+$")
  7.  
  8. for row in db.fetchall():
  9.     id, type, punished_id, punished_name, punisher_id, punisher_name, remover_id, remover_name, start_time, end_time, removal_time, reason, removal_reason = row
  10.  
  11.     if type == 'Unknown':
  12.         continue
  13.  
  14.     if end_time == 0 or end_time == start_time:
  15.         length = 0
  16.     else:
  17.         length = end_time - start_time
  18.  
  19.     if punisher_id == 'Server':
  20.         punisher_id = 'Console'
  21.  
  22.     rowsToInsert.append((type.lower(), punished_name, punished_id, "", punisher_name, punisher_id, remover_id, remover_name, start_time, length, removal_time, reason, removal_reason, -1))
  23.  
  24. insertQuery = "INSERT INTO sourcepunish_punishments (Punish_Type, Punish_Player_Name, Punish_Player_ID, Punish_Player_IP, Punish_Admin_Name, Punish_Admin_ID, UnPunish_Admin_ID, UnPunish_Admin_Name, Punish_Time, Punish_Length, UnPunish_Time, Punish_Reason, UnPunish_Reason, Punish_Server_ID) VALUES "
  25. for row in rowsToInsert:
  26.     insertQuery += '('
  27.     for i in range(0, len(row)):
  28.         element = row[i]
  29.  
  30.         if isinstance(element, str):
  31.             insertQuery += '\'' + conn.escape_string(element) + '\''
  32.         elif isinstance(element, int) or isinstance(element, long):
  33.             insertQuery += str(element)
  34.  
  35.         if i + 1 != len(row):
  36.             insertQuery += ', '
  37.     insertQuery += '),\n'
  38.  
  39. insertQuery = insertQuery[:-2] + ';'
  40. print insertQuery
  41.  
  42. if raw_input("Are you sure? [y/N] ") == 'y':
  43.     db.execute(insertQuery)
  44.     print 'Done!'
  45. else:
  46.     print 'Okay, exiting.'
Advertisement
Add Comment
Please, Sign In to add comment