Advertisement
Guest User

Untitled

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