[Tutor] date matching with python and sqlite3

Che M pine508 at hotmail.com
Sat Sep 1 18:51:13 CEST 2007


Re: date matching with python and sqlite3

>I sorry, maybe I am stupid at the moment but I cannot follow your
>question,....

I'm sorry, I didn't describe that too clearly...have coffee now, let's
try that again.

The user will have a choicebox that has options of "today", "this week", 
"this month",
etc., as well as a way to just provide a range of dates, e.g. "Jan 1 
2007--Apr 15 2007".
The point is I want them to be able to get data from the database for 
whatever time
period they want.  Let's say the data is type of fruit they ate on various 
dates.  So
they would put in "this month" and it would return all the fruit that was 
eaten that
month.  And every time they indicate they ate a fruit, they enter it into 
the database
and Python will save it as a datetime object, which has the format 
2007-09-01 12:00:00.
My trouble is in how to write the SQL statements to match the date(s) they 
need
while ignoring the time part of the datetime.

For example, the code I have for matching to "today" doesn't work because it 
will
match a date saved as "2007-09-01" but not "2007-09-01 12:03:03", and it is 
this
2nd format that the datetime object takes.  I also prefer that format, 
actually, in
case I later want to sort by time of day.

Again, this is the non-working and inelegant code:

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

I'm sure there is an easy way to do this since both Python and SQLite have
date functions, but I have just had trouble understanding the SQlite 
documentation.
Sorry if this is more an SQLite concern than a Python concern, but it is 
sort of on
the border.  If this is still unclear I will try again.  Thanks!

_________________________________________________________________
Get a FREE small business Web site and more from Microsoft® Office Live! 
http://clk.atdmt.com/MRT/go/aub0930003811mrt/direct/01/



More information about the Tutor mailing list