[Tutor] date matching with python and sqlite3

Che M pine508 at hotmail.com
Sat Sep 1 07:55:34 CEST 2007


I'm trying to allow users to select data from an sqlite database using 
Python by choosing either a date or a range of dates.  I'm stuck at just 
allowing the to select data that entered the database "today" and return 
values from a column called duration.  I have this mess at the moment:

#assume they have already chosen this self.datechoice to be today

if self.datechoice == "today":
    todaystring = str(datetime.datetime.today())
    today = todaystring[0:10]
    cur.execute('SELECT duration FROM datatable WHERE date =' + '"' + today 
+ '"')

The 3rd line is a way to take just the first part 10 chars of the 
datetime.today string, so
instead of "2007-09-01 12:00:03" it would be just "2007-09-01", since I just 
want
to match it to today, not a particular time during today.  But this only 
works if the
dates have been saved that way--typically they are saved with the time as 
well, so
this method is not good and obviously not the right way to do it.

I can tell I am going about this totally wrongly and that I should be able 
to use either
the Python datetime functions or SQLite's date functions much better, so any 
insight
would be appreciated.   Thank you.

_________________________________________________________________
A place for moms to take a break! 
http://www.reallivemoms.com?ocid=TXT_TAGHM&loc=us



More information about the Tutor mailing list