Oct-10-2022, 07:25 PM
(This post was last modified: Oct-10-2022, 07:25 PM by deanhystad.)
Did you run my example? What it does is pretty obvious when you run the example.
The scheduler knows when to run an event because you pass the scheduled time as one of the arguments to schedule.enter() or schedule.enterabs(). enter(delta) schedules the event to run when the clock reaches current_time + delta. enterabs(abs_time) schedules the event to run when the clock reaches abs_time.
You don't need to call cursor.commit() unless you did something that changes the database. SELECT does not change the database.
You don't need to use fetchall().
Instead of this:
The scheduler knows when to run an event because you pass the scheduled time as one of the arguments to schedule.enter() or schedule.enterabs(). enter(delta) schedules the event to run when the clock reaches current_time + delta. enterabs(abs_time) schedules the event to run when the clock reaches abs_time.
You don't need to call cursor.commit() unless you did something that changes the database. SELECT does not change the database.
You don't need to use fetchall().
Instead of this:
connection = sqlite3.connect(MainDatabase) cursor = connection.cursor() # Get the Reminder & it's scheduled Date & Time cursor.execute("SELECT Reminder, Date, Time FROM Reminders") Results = cursor.fetchall() connection.commit() connection.close() #For each reminder, add an event to the schedule (enter/enterabs) for row in Results: print(row)Do this:
connection = sqlite3.connect(MainDatabase) cursor = connection.cursor() # Get the Reminder & it's scheduled Date & Time for row in cursor.execute("SELECT Reminder, Date, Time FROM Reminders") print(row) connection.close()You really don't even need the cursor.
connection = sqlite3.connect(MainDatabase) # Get the Reminder & it's scheduled Date & Time for row in connection.execute("SELECT Reminder, Date, Time FROM Reminders"): print(row) connection.close()Why are you saving the schedule time as Date, Time instead of DateTime? I don't know what you have for Date or Time, so in the code below I use a magical function that combines date and time to give you datetime.
def send_reminder(message, scheduled_time): """Function called when time to send reminder""" print(f"{scheduled_time}: {message} def schedule_reminder(reminder, date, time): """Schedule a reminder""" scheduled_time = date_and_time_to_datetime(date, time) schedule.enterabs(schedule_time.timestamp(), send_reminder, arguments=(reminder, scheduled_time)) ... #somewhere in the startup portion of the main code # Schedule all the reminders schedule = sched.scheduler(time.time, time.sleep) connection = sqlite3.connect(MainDatabase) for reminder in connection.execute("SELECT Reminder, Date, Time FROM Reminders"): schedule_reminder(*reminder) connection.close() ... # somewhere later in your main code while True: schedule.run(False) # do other stuff