增加防止插入重复数据的优化

main
roger 2 years ago
parent 3cb37d04d0
commit a64bb0156c
  1. 26
      red_forecast.py

@ -117,6 +117,15 @@ def get_red_forecast(_all_data, _red_data_dict, _red_data, range_num=1):
return _red_forecast_dict, _red_forecast_list
def get_last_date_id():
conn = pymysql.connect(**db)
curser = conn.cursor(DictCursor)
curser.execute("SELECT * FROM history ORDER BY id DESC LIMIT 1")
_data = curser.fetchone()
# print(all_data)
return _data['dateId']
def update_red_forecast_db():
all_data = get_all_data()
red_data = get_all_red()
@ -128,12 +137,17 @@ def update_red_forecast_db():
red_suggest = json.dumps(sorted(red_forecast_list[0:6]))
conn = pymysql.connect(**db)
curser = conn.cursor(DictCursor)
curser.execute()
curser.execute(
"INSERT INTO lottery.red_forecast (`dateId`, `params`, `red_rate`, `red_suggest`) VALUES (%s, %s, %s, %s)",
(date_id, params, red_rate, red_suggest))
conn.commit()
curser.close()
last_date_id = get_last_date_id()
curser.execute("SELECT * FROM lottery.red_forecast WHERE dateId = %s", (last_date_id, ))
have_duplicate = False if len(curser.fetchall()) == 0 else True
if not have_duplicate:
curser.execute(
"INSERT INTO lottery.red_forecast (`dateId`, `params`, `red_rate`, `red_suggest`) VALUES (%s, %s, %s, %s)",
(date_id, params, red_rate, red_suggest))
conn.commit()
curser.close()
else:
print('数据库中有重复记录,无需重复插入')
if __name__ == "__main__":

Loading…
Cancel
Save