Reading back a datetime in sqlite3

后端 未结 1 398
遇见更好的自我
遇见更好的自我 2021-01-02 13:35

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 r

相关标签:
1条回答
  • 2021-01-02 14:15

    You have to set detect_types to sqlite.PARSE_COLNAMES and use as "foo [timestamp]" like this:

    import sqlite3
    import datetime
    
    db = sqlite3.connect(':memory:', detect_types = sqlite3.PARSE_COLNAMES)
    c = db.cursor()
    c.execute('create table foo (bar integer, baz timestamp)')
    c.execute('insert into foo values(?, ?)', (23, datetime.datetime.now()))
    c.execute('insert into foo values(?, ?)', (42, datetime.datetime.now() + datetime.timedelta(-1)))
    c.execute('select bar, baz as "ts [timestamp]" from foo')
    print c.fetchall()
    c.execute('select max(baz) as "ts [timestamp]" from foo')
    print c.fetchall()
    

    Did a nice little Google search and found this message.

    0 讨论(0)
提交回复
热议问题