Advertisement
Guest User

Untitled

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