在sqlite3中读回datetime [英] Reading back a datetime in sqlite3

查看:209
本文介绍了在sqlite3中读回datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python创建一个内存时间戳列的sqlite3数据库。当我在查询中对此列使用min()或max()时,列作为字符串而不是Python datetime对象返回。我在Stackoverflow上阅读了一个上一个问题为正常的SELECT语句提供了一个解决方案,但是如果使用max()或min())则不起作用。以下是一个例子:

 >>> 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对象在0x7eff420e0be0>
>>> c.execute('select * from foo')
< sqlite3.Cursor对象在0x7eff420e0be0>
>>> c.fetchall()
[(23,datetime.datetime(2010,12,14,1,15,54,685575))]
>>> c.execute('从foo'中选择max(baz)'
< sqlite3.Cursor对象在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()手动转换字符串?

解决方案

你必须设置detect_types到sqlite.PARSE_COLNAMES并使用作为foo [timestamp]像这样:

  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 asts [timestamp]from foo')
print c.fetchall()
c.execute('select max(baz)asts [timestamp]from foo')
打印c.fetchall()

做了一个很好的Google搜索,发现这个消息


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?

解决方案

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.

这篇关于在sqlite3中读回datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆