KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am using Python to create an in-memory sqlite3 database with a timestamp column. When I use min() or max() on this column in my query, the column is returned as a string rather than a Python datetime object. I read a previous question on Stackoverflow that provided a solution for normal SELECT statements, but it doesn't work if max() or min() is used. Here's an example: >>> db = sqlite3.connect(':memory:', detect_types=sqlite3.PARSE_DECLTYPES) >>> c = db.cursor() >>> c.execute('create table foo (bar integer, baz timestamp)') <sqlite3.Cursor object at 0x7eff420e0be0> >>> c.execute('insert into foo values(?, ?)', (23, datetime.datetime.now())) <sqlite3.Cursor object at 0x7eff420e0be0> >>> c.execute('select * from foo') <sqlite3.Cursor object at 0x7eff420e0be0> >>> c.fetchall() [(23, datetime.datetime(2010, 12, 14, 1, 15, 54, 685575))] >>> c.execute('select max(baz) from foo') <sqlite3.Cursor object at 0x7eff420e0be0> >>> c.fetchall() [(u'2010-12-14 01:15:54.685575',)] I tried to cast the result to a timestamp but it only returns the year: >>> c.execute('select cast(max(baz) as timestamp) from foo') <sqlite3.Cursor object at 0x7eff420e0be0> >>> c.fetchall() [(2010,)] Is there any way to fetch a proper datetime object, without manually converting the string using datetime.strptime() after fetching it?
Tags (comma-separated)
Save Edits
Cancel