Advertisement
Guest User

Untitled

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