-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder_fixer.py
61 lines (43 loc) · 1.55 KB
/
order_fixer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import mysql.connector
import schedule
import time
import mysql.connector
from datetime import datetime, timedelta
from tabulate import tabulate
def clean_orders():
cnx = mysql.connector.connect(user='root', password='Namit@123', host='localhost', port='3306',
database='online retail store')
cursor = cnx.cursor()
select_query = """
SELECT * FROM `online retail store`.`order`
WHERE orderID NOT IN (SELECT orderID FROM billing);
"""
# Execute the select query
cursor.execute(select_query)
records = cursor.fetchall()
# Print the records that will be deleted
if records:
print(f"Records to be deleted at {time.strftime('%Y-%m-%d %H:%M:%S')}:")
for record in records:
print(record)
# SQL command to delete orders without corresponding billing records
delete_query = """
DELETE FROM `online retail store`.`order`
WHERE orderID NOT IN (SELECT orderID FROM billing);
"""
cursor.execute(delete_query)
cnx.commit()
print(f"Deleted {cursor.rowcount} records.")
else:
print(f"No records to delete at {time.strftime('%Y-%m-%d %H:%M:%S')}")
cursor.close()
def job():
print("Running clean up job...")
clean_orders()
def scheduled_job_1():
# Schedule the job to run daily at midnight
schedule.every(10).seconds.do(job)
# Keep the script running in a loop
while True:
schedule.run_pending()
time.sleep(1)