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.
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"
results = cursor.fetchall()
for row in results:
code = row
ArrivalDate = row
DepartureDate = row
print "code=%s,ArrivalDate=%s,DepartureDate=%s" %\
(code, ArrivalDate, DepartureDate )
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])