Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85772

Re: sqlite3 and dates

From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: sqlite3 and dates
Date 2015-02-18 07:47 +0000
References <mc1atd$cq0$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.18806.1424245698.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 18/02/2015 06:19, Frank Millman wrote:
> Hi all
>
> sqlite3 does not have a DATE type, but the python module does a pretty good
> job of providing one -
>
>>>> import sqlite3
>>>> conn = sqlite3.connect(':memory:', detect_types=sqlite3.PARSE_DECLTYPES)
>>>> cur = conn.cursor()
>>>> cur.execute('CREATE TABLE test (dob DATE)')
> <sqlite3.Cursor object at 0x00FE9BE0>
>>>> cur.execute('INSERT INTO TEST (dob) VALUES (?)', ('2015-03-31',))
> <sqlite3.Cursor object at 0x00FE9BE0>
>>>> cur.execute('SELECT * FROM test')
> <sqlite3.Cursor object at 0x00FE9BE0>
>>>> cur.fetchone()
> (datetime.date(2015, 3, 31),)
>>>>
>
> However, the following does not return a date object -
>
>>>> cur.execute('SELECT CAST(? AS DATE)', ('2015-03-31',))
> <sqlite3.Cursor object at 0x00FE9BE0>
>>>> cur.fetchone()
> (2015,)
>>>>
>
> I don't know how easy this would be to implement, but it would be nice if it
> could be made to work.
>
> Is it worth filing a feature request?
>
> Frank Millman
>

Will this do?

cur.execute('select current_date as "d [date]", current_timestamp as "ts 
[timestamp]"')
row = cur.fetchone()
print("current_date", row[0], type(row[0]))
print("current_timestamp", row[1], type(row[1]))

https://docs.python.org/3/library/sqlite3.html#default-adapters-and-converters

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: sqlite3 and dates Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-18 07:47 +0000

csiph-web