我编写了以下程序来获取我的数据库的一些行,其中包含有关22-Jan-1963
之后出生的用户的信息:
import datetime as dt
import peewee as pw
db = pw.SqliteDatabase('people.db')
class Person(pw.Model):
name = pw.CharField()
birthday = pw.DateField(formats=['%d-%b-%Y'])
class Meta:
database = db # This model uses the "people.db" database.
db.create_tables([Person])
bob = Person(name = 'Bob', birthday = '21-Jan-1960')
james = Person(name = 'James', birthday = '22-Jan-1965')
steve = Person(name = 'Steve', birthday = '20-Jan-1970')
alex = Person(name = 'Alex', birthday = '18-Jan-1975')
bob.save()
james.save()
steve.save()
alex.save()
for item in Person.select().where(Person.birthday > dt.date(1963,1,22)):
print item.name,item.birthday, item.birthday > dt.date(1963,1,22)
但是当我运行它时,输出不是我预期的(我希望输出中有 James,Steve和Alex ):
>>> ================================ RESTART ================================
>>>
Bob 1960-01-21 False
James 1965-01-22 True
Steve 1970-01-20 True
>>>
好吧,我在dt.date(1963,1,22)
方法中将"22-Jan-1963"
替换为where()
,现在结果为:
>>> ================================ RESTART ================================
>>>
James 1965-01-22 True
>>>
如上所述,它仍然不正确。
我该怎么办?
答案 0 :(得分:3)
我绝对不认识PeeWee,但鉴于Sqlite没有原生日期时间格式(它将其模仿为字符串),您可能想尝试将日期格式更改为"%Y-%m-%d"
;这将自动正确地作为字符串排序,这可能适用于Sqlite。
答案 1 :(得分:1)
在documentation之后,我建议
other = dt.date(1963, 1, 22)
Person.select().where(
Person.birthday.year >= other.year
).where(
Person.birthday.year > other.year
| Person.birthday.month >= other.month
).where(
Person.birthday.year > other.year
| Person.birthday.month > other.month
| Person.birthday.day > other.day
)
是的,这很啰嗦......