7
私は、タイムスタンプの列を持つメモリ内のsqlite3データベースを作成するためにPythonを使用しています。クエリでこの列に対してmin()またはmax()を使用すると、列はPython datetimeオブジェクトではなく文字列として返されます。私はprevious question on Stackoverflow通常のSELECTステートメントの解決策を提供したが、max()またはmin()が使用されている場合は動作しません。ここでは例です:sqlite3でdatetimeを読み取る
>>> 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',)]
私は、タイムスタンプに結果をキャストしようとしましたが、それだけで年を返します:
>>> c.execute('select cast(max(baz) as timestamp) from foo')
<sqlite3.Cursor object at 0x7eff420e0be0>
>>> c.fetchall()
[(2010,)]
は、手動で文字列を変換することなく、適切なDateTimeオブジェクトを取得する方法はありますそれをフェッチした後にdatetime.strptime()を使用しますか?
ありがとう! – del