Solved

Date Range list from database

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Instering to MySQL table 5 48
Difference between List and Array in Python 5 65
possible to record changes (trigger I think) msql 11 35
two ways encryption with php 3 28
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 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…

773 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