我的Android sqlite DB中有这个表:
CREATE TABLE statistics (subject TEXT,hits INTEGER,fails INTEGER,date DATE)
日期字段在每个寄存器中存储日期时间(‘now’,’localtime’).
现在我必须查询最后一天,上周和上个月的寄存器以显示一些统计信息.
我一直在尝试这样的事情
SELECT Timestamp,datetime('now','-1 week') FROM statistics WHERE TimeStamp < datetime('now','-1 week')
和这个
SELECT * FROM statistics WHERE date BETWEEN datetime('now',localtime') AND datetime ( 'now','-1 month')
并不起作用:(
我该怎么做?
我可以通过简单地在虚拟设备模拟器中转发日期来检查查询是否正常吗?
谢谢!
解决方法
我找到了这个解决方案.我希望这个对你有用.
最后一天:
SELECT * FROM statistics WHERE date BETWEEN datetime('now','start of day') AND datetime('now','localtime');
上周:
SELECT * FROM statistics WHERE date BETWEEN datetime('now','-6 days') AND datetime('now','localtime');
上个月:
SELECT * FROM statistics WHERE date BETWEEN datetime('now','start of month') AND datetime('now','localtime');