?
Solved

Date Range list from database

Posted on 2011-03-23
3
Medium Priority
?
423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 25

Assisted Solution

by:clockwatcher
clockwatcher earned 800 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 1200 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this article, we’ll look at how to deploy ProxySQL.
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 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…
Suggested Courses

770 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