Question: How can I pull data in mySQL by day using python date?
Say I want `day1` and `day2` ( or a day after `day1` ) iterate for `n` times 

So I need the date in "where" SQL statement to look like below list in each iteration (`n` times )

```
day1 >= '2012-01-01'  and  day2 < '2012-01-02'    ( n = 1 )
day1 >= '2012-01-02'  and  day2 < '2012-01-03'    ( n = 2 )
.
.
day1 >= yesterday    and day2  < today            ( n times ) 

```

.

```
Start_date = '2012-01-01'   <- How can I write this in python
End_date = Today()   <- and this 

```

So as to write:

```
for each iteration ..
    con.execute("select * from table where date >= day1 and date < day2" )

```

Answer: 
Use the `datetime` class with the `strftime` function. 

The `datetime` class is used to build an object which represents a specific date and time. The `strftime` function converts it to a specific string, according to the formatting you choose.

According to [MySQL's documentation](http://dev.mysql.com/doc/refman/5.5/en/datetime.html), the standard datetime formatting is `YYYY-MM-DD HH:MM:SS`. 

Here's an example that should work:

```
day1 = datetime.datetime(2012, 1, 1).strftime('%Y-%m-%d %H:%M:%S')
day2 = datetime.datetime(2012, 1, 2).strftime('%Y-%m-%d %H:%M:%S')
con.execute("select * from table where date >= %s and date < %s", (day1, day2))

```

If you want to make additional queries, just create appropriate `datetime.datetime` objects on each round of your loop. For instance:

```
for i in xrange(1, 10):
    # ...
    day2 = datetime.datetime(2012, 1, i).strftime('%Y-%m-%d %H:%M:%S')
    # ...

```
Use [datetime.date](http://docs.python.org/library/datetime.html#date-objects) objects. They are wonderful things because with them you can:

* easily compute today (`dt.date.today()`),
* easily compute the next day (`start + dt.timedelta(days = 1)`,
* compare dates (e.g. `start < end`)
* feed them *directly* into `con.execute`. There is no
need to pre-format them as strings.

---

```
import datetime as dt
start = dt.date(2012,1,1)
end = dt.date.today()

while start < end:
    nextday = start + dt.timedelta(days = 1)
    con.execute("select * from table where date >= %s and date < %s",
                (start, nextday))

    start = nextday

```