import pymysql import json from functools import reduce from pymysql.cursors import DictCursor db = { 'host': 'home.rogersun.online', 'user': 'root', 'password': 'Sxzgx1209', 'database': 'lottery' } real_blue = ["01", "04", "09", "10", "20", "33"] def get_real_data(): conn = pymysql.Connect(**db) cursor = conn.cursor(DictCursor) cursor.execute("SELECT * FROM lottery.history ORDER BY id DESC LIMIT 1") history_data = cursor.fetchone() # print(history_data) # [{'id': 2979, 'dateId': '2023035', 'openDate': datetime.date(2023, 3, 30), 'red': '["01", "04", "09", "10", "20", "33"]', 'blue': '06'}] return history_data def general_blue_index(_history_data): conn = pymysql.Connect(**db) cursor = conn.cursor(DictCursor) cursor.execute("SELECT * FROM lottery.blue_forecast WHERE dateId = %s", (str(int(_history_data['dateId']) - 1)), ) history_blue = cursor.fetchall() # print(history_blue) for data in history_blue: n = 1 for k, v in json.loads(data['blue']).items(): if k == _history_data['blue']: real_index = {'real_blue': _history_data['blue'], 'real_index': n} cursor.execute("UPDATE lottery.blue_forecast SET comment = %s WHERE id = %s", (json.dumps(real_index), data['id'])) break else: n += 1 conn.commit() cursor.close() def general_red_index(_history_data): conn = pymysql.Connect(**db) cursor = conn.cursor(DictCursor) cursor.execute("SELECT * FROM lottery.red_forecast WHERE dateId = %s", (str(int(_history_data['dateId']) - 1)), ) history_red = cursor.fetchone() # print(history_red) real_red = json.loads(_history_data['red']) real_red_index = [] n = 1 for red, _ in json.loads(history_red['red_rate']).items(): if red in real_red: real_red_index.append(n) n += 1 else: n += 1 # print(_history_data['red'], json.dumps(real_red_index), history_red['id']) cursor.execute("UPDATE lottery.red_forecast SET real_result = %s, comments = %s WHERE id = %s", (_history_data['red'], json.dumps(real_red_index), history_red['id'])) conn.commit() cursor.close() if __name__ == "__main__": history = get_real_data() general_blue_index(history) general_red_index(history)