Advertisement
Guest User

Untitled

a guest
Oct 30th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. import pymysql
  2.  
  3. def free_num(unix_times, num):
  4.     if num in unix_times:
  5.         return free_num(unix_times, num+1)
  6.     else:
  7.         return num
  8.  
  9. def update_db(old_num, new_num):
  10.     connection = pymysql.connect(host='localhost',
  11.                              user='root',
  12.                              password='01xen2012',
  13.                              db='zabbix',
  14.                              charset='utf8mb4',
  15.                              cursorclass=pymysql.cursors.DictCursor)
  16.     with connection.cursor:
  17.         sql = "UPDATE history_log SET clock="+new_num+" WHERE clock="+old_num+" LIMIT 1"
  18.         curs.execute(sql)
  19.  
  20.  
  21.  
  22. # Connect to the database
  23. connection = pymysql.connect(host='localhost',
  24.                              user='root',
  25.                              password='01xen2012',
  26.                              db='zabbix',
  27.                              charset='utf8mb4',
  28.                              cursorclass=pymysql.cursors.DictCursor)
  29.  
  30.  
  31. with connection.cursor() as cursor:
  32.     sql = "SELECT id, clock from history_log"
  33.     cursor.execute(sql)
  34.     all_history = cursor.fetchall()
  35.     unix_times = []
  36.     for r in all_history:
  37.         unix_times.append(r['clock'])
  38.     unix_times = set(unix_times)
  39.  
  40.  
  41.  
  42. with connection.cursor() as cursor2:
  43.     sql = "SELECT count('*') as c, clock from history_log GROUP BY clock HAVING c>1"
  44.     cursor2.execute(sql)
  45.     dublicates = cursor2.fetchall()
  46.     for d in dublicates:
  47.         dublicate_count = d['c']
  48.         while dublicate_count>1:
  49.             dublicate_count-=1
  50.             new_num = free_num(unix_times, d['clock'])
  51.             if new_num != d['clock']:
  52.                 unix_times.add(new_num)
  53.                 update_db(d['clock'], new_num)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement