Solved

Date Range list from database

Posted on 2011-03-23
3
417 Views
Last Modified: 2012-06-22
I would like to loop through a database table and insert new records for all dates between Arrival and Departure dates from the source into a new database table, I have the database select call the records and have a function looping through a start and finish date but cannot seem to get the start and finish date from the database records and loop through the date range.

The code I have is looping through the database and printing out the records it finds so I could see that it was working and printing out the date_range loop to make sure that was working.
import MySQLdb
import datetime 

DB_NAME = 'dbname'
DB_SERVER = 'localhost'
DB_USER = 'user'
DB_PASS = 'pword'

db = MySQLdb.connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME)
cursor = db.cursor()
sql = "SELECT Code, ArrivalDate, DepartureDate FROM planner"

try:
    cursor.execute(sql)
    results = cursor.fetchall()
    for row in results:
        code = row[0]
        ArrivalDate = row[1]
        DepartureDate = row[2]
        print "code=%s,ArrivalDate=%s,DepartureDate=%s" %\
    (code, ArrivalDate, DepartureDate )
 
except:
    print "error: unable to fetch data"
def date_range(start, end): 
    r = (end+datetime.timedelta(days=1)-start).days 
    return [start+datetime.timedelta(days=i) for i in range(r)] 
  
start = datetime.date(2008,01,01) 
end = datetime.date(2008,02,01) 
dateList = date_range(start, end) 
print '\n'.join([str(date) for date in dateList]) 
db.close()

Open in new window

0
Comment
Question by:chrispaton
3 Comments
 
LVL 25

Assisted Solution

by:clockwatcher
clockwatcher earned 200 total points
ID: 35200385
It should be possible to do that with an insert query directly.  Is there a reason that you don't want to do it that way?

insert into newtable (code, arrivaldate, departuredate) 
  select code, arrivaldate, departuredate from planner 
  where 
       arrivaldate between '2008-01-01' and '2008-01-02' or 
       departuredate between '2008-01-01' and '2008-01-02'

Open in new window


0
 

Author Comment

by:chrispaton
ID: 35201664
I dont have the all of the dates so cannot do it that way, I only have the Arrival and Departure date and I need to write to the new table each individual date between them.

Example in source table;
Code - IDR2234
Arrival - 2008-01-01
Departure - 2008-01-07

I need 2008-01-01, 2008-01-02, 2008-01-03, 2008-01-04, 2008-01-05,2008-01-06, 2008-01-07


0
 
LVL 29

Accepted Solution

by:
pepr earned 300 total points
ID: 35202520
Create one auxiliary table with all the dates for the reasonable time.  (One year has 365 days, 10 years have 3652 days, 100 years about 37000 days. This is not that much for one static table. Then:

  SELECT date FROM auxDateTab WHERE date BETWEEN Arrival AND Departure

... and you have the range between the dates.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question