Eliminate "extra" variable

Tim Chase python.list at tim.thechases.com
Sun Dec 8 17:18:58 EST 2013


On 2013-12-08 12:58, Igor Korot wrote:
> Also, the data comes from either SQLite or mySQL and so to eliminate
> the difference between those engines dates are processed as strings
> and converted to dates for the calculation purposes only.
> Maybe I will need to refactor SQLite processing to get the dates as
> dates and not a string, but that's probably for the future. so that
> dates will be kept as the datetime type until the end of the
> function. As I wrote the dates will be used as the text for the
> plotting window axis labels and as the labels they should come out
> as strings, hence the conversion.

Sqlite can do this automatically if you tell it to upon connecting:
    
>>> import sqlite3
>>> conn = sqlite3.connect('x.sqlite',
...    detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
>>> cur.execute("create table foo (s date);")
<sqlite3.Cursor object at 0x7f7f31665570>
>>> import datetime
>>> today = datetime.date.today()
>>> cur.execute("insert into foo(s) values (?)", (today,))
<sqlite3.Cursor object at 0x7f7f31665570>
>>> cur.execute("select * from foo")
<sqlite3.Cursor object at 0x7f7f31665570>
>>> r = cur.fetchone()
>>> print r
(datetime.date(2013, 12, 8),)


Note that it returns a datetime.date, the same as it was defined.

-tkc







More information about the Python-list mailing list