Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymysql.cursors
- # Connect to the database
- connection = pymysql.connect(host='localhost',
- user='user',
- password='passwd',
- db='db',
- charset='utf8mb4',
- cursorclass=pymysql.cursors.DictCursor)
- with connection.cursor() as cursor:
- sql = "SELECT id, clock from history_log"
- cursor.execute(sql)
- all_history = cursor.fetchall()
- unix_times = []
- for r in all_history:
- unix_times.append(r['clock'])
- unix_times = set(unix_times)
- with connection.cursor() as cursor2:
- sql = "SELECT count('*') as c, clock from history_log GROUP BY clock HAVING c>1"
- cursor2.execute(sql)
- dublicates = cursor.fetchall()
- for d in dublicates:
- dublicate_count = d['c']
- while dublicate_count>1:
- dublicate_count-=1
- new_num = free_num(unix_times, d['clock'])
- if new_num != d['clock']
- unix_times.append(new_num)
- update_db(d['clock'], new_num)
- def free_num(unix_times, num):
- if num in unix_times:
- return free_num(unix_times, num+1)
- else:
- return num
- def update_db(old_num, new_num) :
- with connection.cursor:
- sql = "UPDATE history_log SET clock="+new_num+" WHERE clock="+old_num+" LIMIT 1"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement