[Tutor] trouble with dates and python and databases

Christian Witts cwitts at compuscan.co.za
Fri Mar 12 07:27:16 CET 2010


Christopher Spears wrote:
> I'm trying to write a script that calculates the rate of disk usage.  I think the best way to accomplish this task is to write a script that will monitor a server's capacity and how much space is being used on a daily basis and store the information in a SQLite database.  Then the program can retrieve the necessary information from the database to compute the rate.
>
> Server 1
> 3/11/10  10 GB Used/50 GB Capacity
> 3/12/10  15 GB Used/50 GB Capacity
> 3/13/10  17 GB Used/50 GB Capacity
>
> Rate of usage = 7 GB / 3 days = 2.3 GB per day
>
> Eventually, I want the script to issue a warning if the server is in danger of becoming full in a certain number of days.
>
> My problem is I'm not sure how to store and retrieve the dates.  I assume the best way to record the date is to use datetime.date.
>
>   
>>>> import datetime
>>>> datetime.date.today()
>>>>         
> datetime.date(2010, 3, 11)
>
> How could I pass the datetime object into the database?
>
> Any advice would be appreciated!
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>   

cursor.execute("""select  *
                  from    logs
                  where   log_date = to_date(:bind_variable, 'yyyymmdd')
               """, bind_variable = your_date_here)

I use that style most commonly, but remember if you insert the records 
into the database using SYSDATE you will need to call `trunc` on your 
log_date field in order for it to match your input.

If for example you want the last 3 days of data though to trend on you 
can always do your select like

select  log_date, usage, capacity
from    logs
where   log_date >= trunc(sysdate) - 3
order by log_date asc

-- 
Kind Regards,
Christian Witts




More information about the Tutor mailing list