Solved

Date Range list from database

Posted on 2011-03-23
3
413 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 28

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now