我用SQLite Date Browse
应用程序创建表...
当我想datetime
从timestamp
列中检索值时,SQLite返回unicod类型...
这是我的插入代码:
def Insert(self,mode,path,vname,stime,ftime):
con = sqlite3.connect(PATH_DataBase) # @UndefinedVariable
con.execute('INSERT INTO SendList VALUES(?,?,?,?,?)',(mode,path,vname,stime,ftime))
con.commit()
con.close()
dt1 = datetime.datetime(2013,01,01,01,01,01,0)
dt2 = datetime.datetime(2015,01,01,01,01,01,0)
c = 0
for f in os.listdir('/home/abbas/test/'):
c += 1
slist.Insert(common.MODE_Bluetooth_JAVA, '/home/abbas/test/'+f,'flower'+str(c) , dt1, dt2)
Run Code Online (Sandbox Code Playgroud)
现在这是我的桌子:
但是当我想starttime
与datetime.now()python 比较时,给我错误:
TypeError: can't compare datetime.datetime to unicode
“ SQLite没有预留存储类来存储日期和/或时间。” 参考:https : //www.sqlite.org/datatype3.html
Python的sqlite3模块提供了“ datetime模块中日期和datetime类型的默认适配器”。参考:https : //docs.python.org/2/library/sqlite3.html#default-adapters-and-converters
唯一要注意的是,您必须确保正确定义列。DDL示例:
import sqlite3
con = sqlite3.connect(PATH_DataBase, detect_types=sqlite3.PARSE_DECLTYPES)
con.execute('''create table if not exists SendList (
cid primary key,
mode text,
path text,
vname text,
starttime timestamp,
endtime timestamp);''')
con.commit()
con.close()
Run Code Online (Sandbox Code Playgroud)
任何随后的插入或选择数据连接都必须sqlite3.PARSE_DECLTYPES
作为关键字参数(aka kwarg)的值传递detect_types
。例:
import datetime as dt
con = sqlite3.connect(PATH_DataBase, detect_types=sqlite3.PARSE_DECLTYPES)
cur = con.cursor()
cur.execute('''select
*
from
SendList
where
starttime between ? and ?
limit 10;''',
(dt.datetime(2013,1,1,0,0,0), dt.datetime(2014,12,31,23,59,59)))
results = cur.fetchall()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4984 次 |
最近记录: |