Solved

Date Range list from database

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
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…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

867 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

23 Experts available now in Live!

Get 1:1 Help Now