Click here to Skip to main content
15,886,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to make a notification depending on the current date, and I have a date list from the database, so when the current date matches any date from the date list so the GUI will display a notification message box that includes that date and some other info from the same row that has the date that matches the current date.

Problem: How to select and fetch values from the database, by depending on the date column so I can insert the date and the other values from that specific date, in the notification message box.

What I have tried:

# Importing DateEntry from tkcalendar to store dates in the database:

Python
import datetime
from tkcalendar import DateEntry,Calendar
from datetime import datetime,timedelta


Python
self.buying_registration_Date_E = DateEntry(self.buying_payment_page,justify="right", bd=3, width=35, selectmode='day',headersbackground="#487FB9",headersforeground="#FFFFFF", date_pattern='dd-mm-yyyy',textvariable=self.buying_registration_Date_var)



code: #making the date list by selecting registration_check_date from DB

self.buyingrigstrationdates = []
self.buying_registrationdates=self.cursorObj.execute
('SELECT registration_check_date, car_type FROM 
buying_rigistration_date order by 
registration_check_date')
self.buying_registrationdates_output=self.cursorObj.
fetchall()
for t in self.buying_registrationdates_output:
     for self.buying_registrationdates_output in t:         
       self.buyingrigstrationdates.append
      (self.buying_registrationdates_output)
self.con.commit()

date = datetime.now()
now = date.strftime('%d-%m-%Y')
print(now)
#Trying to select the values from a row where is there the current date in it, but it doesn't work and I got an empty list.

 if now in self.buyingrigstrationdates:
    self.dateinfo = self.cursorObj.execute("SELECT * 
    FROM buying_rigistration_date WHERE 
    strftime('%Y',registration_check_date) = 
    strftime('%Y','now')")
    self.dateinfo_fetch = self.cursorObj.fetchall()
        messagebox.showinfo("rigistration", 
    f"Rigistration date{self.dateinfo_fetch}")
Posted
Updated 6-May-22 10:53am
v3

1 solution

Why do you need to read all the database records and then search for any with today's date? A much simpler method would be:
Python
date = datetime.now()
# you may need to convert the date to the format stored in the DB
now = date.strftime('%d-%m-%Y')
cursor.execute('SELECT registration_check_date, car_type FROM buying_rigistration_date WHERE registration_check_date=?', (now,))
 
Share this answer
 
v4
Comments
asaad kittaneh 3-May-22 8:02am    
@Richard MacCutchan thank you for your reply, I tried it before and it didn't work, to clarify the situation more, I have used DateEntry from tkcalendar to insert and store dates on the DB using date_pattern='dd-mm-yyyy' , I edited the question to be more clear for you, if you could take another look at it, I hope the situation will be clearer now.
Richard MacCutchan 3-May-22 8:18am    
You are trying to compare the year with the string "now".
    self.dateinfo = self.cursorObj.execute("SELECT * 
    FROM buying_rigistration_date WHERE 
    strftime('%Y',registration_check_date) = 
    strftime('%Y','now')") # <-- 'now' will never equal a year number.
asaad kittaneh 3-May-22 8:36am    
@Richard MacCutchan iam using now self.dateinfo = self.cursorObj.execute(
'SELECT registration_check_date, car_type FROM buying_rigistration_date WHERE registration_check_date=?', (date,))
but it didn't work too, and to clarify more when I print the date from the Database I got this date pattern ['03-05-2022', 'Alfa Romeo'], is there any solution to comparing the current date with this type date that I fetched from the database
Richard MacCutchan 3-May-22 9:13am    
Sorry, I cannot guess why it does not work. I have some similar code that does exactly what you are trying and it selects the required record correctly.
asaad kittaneh 3-May-22 9:11am    
@Richard MacCutchan I have tried everything and it doesn't work at all, and comparing date=date = datetime.now() with the dates on the database will never work because when printing(date) it shows the date and the time 2022-05-03 16:04:03.135530 and its doesn't work to compare date only with date and time that's why I convert date=datetime.now to date.strftime('%d-%m-%Y')

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900