从sqlite数据库获取上周的信息

时间:2013-08-06 10:36:15

标签: android sqlite

我的项目中有一个sqlite数据库,我有3个yearmonthday个字段。我想从我的表中获取上周或上个月的信息,并使用游标适配器在listView中显示它。我不知道该怎么做。我的表格不是很大,我的记录也按日期排序,因此迭代可以作为答案。请以任何方式帮助我。

2 个答案:

答案 0 :(得分:0)

SELECT * FROM myTable WHERE DATE(timeStamp) >= DATE('now', 'weekday 0', '-7 days');

答案 1 :(得分:0)

如果表格是这样的

CREATE TABLE t(some_data, year, month, day);

INSERT INTO t(some_data, year, month, day)
                    SELECT ABS(RANDOM()%10000), 2013,  8,  6 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  8,  4 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  8, 01 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013, 07, 31 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  7, 30 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  7, 25 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013, 07, 24 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  7, 23 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  7, 20 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  7, 10 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  7,  9
;

这是一种可能的解决方案

SELECT some_data, year
    || '-' ||
    CASE WHEN LENGTH(month) = 2 THEN month
         WHEN LENGTH(month) = 1 THEN 0 || month
    END
    || '-' ||
    CASE WHEN LENGTH(day) = 2 THEN day
         WHEN LENGTH(day) = 1 THEN 0 || day
    END as d
FROM t
WHERE DATE(d) >= DATE('now', '-7 days')
ORDER BY d;


some_data   d         
----------  ----------
7455        2013-07-30
6123        2013-07-31
6903        2013-08-01
9332        2013-08-04
1895        2013-08-06