Advertisement
Guest User

Untitled

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