Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从SQLite存储和检索过去30天的数据?_Android_Sqlite - Fatal编程技术网

Android 如何从SQLite存储和检索过去30天的数据?

Android 如何从SQLite存储和检索过去30天的数据?,android,sqlite,Android,Sqlite,我想从SQLite数据库中检索上周或30天的数据,并将其显示在列表视图中。我尝试了不同的查询,但没有得到结果。那么,我如何才能获得过去30天的数据呢 创建数据库表查询: public String CREATE_DATA_TABLE="CREATE TABLE "+TABLE_DATA+"(" + DATE + " TEXT," + TIME+ " TEXT,"+STEPS + " TEXT,"+CALORIES +" TEXT," + DISTA

我想从SQLite数据库中检索上周或30天的数据,并将其显示在
列表视图中。我尝试了不同的查询,但没有得到结果。那么,我如何才能获得过去30天的数据呢

创建数据库表查询:

public String CREATE_DATA_TABLE="CREATE TABLE "+TABLE_DATA+"("
            + DATE + " TEXT," + TIME+ " TEXT,"+STEPS + " TEXT,"+CALORIES +" TEXT," +
            DISTANCE+ " TEXT"+")";
存储数据:

public boolean storeData(String date,String time,long stepsTaken,
                          long caloriesBurned, String distanceCovered){
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(DATE, date);
        values.put(TIME, time);
        values.put(STEPS, stepsTaken);
        values.put(CALORIES, caloriesBurned);
        values.put(DISTANCE, distanceCovered);
        long result=db.insert(TABLE_DATA,null, values);
        if(result==-1)
            return false;
        else
            return true;
    }
获取数据时,我尝试检索数据的查询位于注释行中:

public ArrayList<User> getData(){

        calendar=Calendar.getInstance();
        dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        date=dateFormat.format(calendar.getTime());

        ArrayList<User> arrayList= new ArrayList<>();
        SQLiteDatabase db=getReadableDatabase();
        //String query="SELECT * FROM "+ TABLE_DATA+" WHERE date BETWEEN datetime('now','-30 days') AND datetime('now', 'localtime')";
        //Cursor cursor=db.rawQuery(query,null);
       Cursor cursor=db.rawQuery("SELECT * FROM "+TABLE_DATA,null);
        while (cursor.moveToNext()){
            String date=cursor.getString(0);
            String time=cursor.getString(1);
            long steps=cursor.getLong(2);
            long calories=cursor.getLong(3);
            String distance=cursor.getString(4);
            User user=new User(date,time,steps,calories,distance);
            arrayList.add(user);
        }


        return arrayList;

    }

您的问题是,由于日期列以日期开始,因此这将是比较的第一个字符,因此*02-01-1900大于01-12-2018。这一点更加复杂,因为您正在使用返回日期为YYYY-MM-DD的datetime函数,因此您将1990年1月1日与2018年11月11日(更少)进行比较。然而,1990年1月21日将高于2018年11月11日(即21日高于20日)

因此,您必须使用相同格式的日期进行比较或排序,以返回与预期一致的结果

您有两个核心解决方案:-

  • 修改代码,以支持的格式存储日期,这恰好也适合比较和排序(即YYYY-MM-DD),请参阅

    • 在这种情况下,如果按照
      String query=“SELECT*FROM”+TABLE_DATA+“其中datee('now'、'localtime'、'-30天')和date('now'、'localtime')之间的日期使用了date函数而不是datetime函数,则查询将起作用
  • 动态重新格式化从“日期”列检索的日期。e、 g.通过使用:-

    从表中选择*数据,其中
    substr(日期,7,4)| | substr(日期,3,4)| | substr(日期,1,2)
    在日期('now'、'localtime'、'-30天)和日期('now'、'localtime')之间
    ;

    • 注意同样,您应该一致地使用localtime(如上所述,在存储值时,禁止从表中检索日期,该日期应该应用localtime)
  • 概念证明 以下是一些可以复制并粘贴到SQLite工具的SQL,以显示上面的第二个选项有效:-

    DROP TABLE IF EXISTS table_data;
    CREATE TABLE IF NOT EXISTS table_data (id INTEGER PRIMARY KEY, date TEXT);
    INSERT INTO table_data (date) VALUES
      (substr(date('now','localtime','-50 days'),9,2)||substr(date('now','localtime','-50 days'),5,4)||substr(date('now','localtime','-50 days'),1,4)),
        (substr(date('now','localtime','-40 days'),9,2)||substr(date('now','localtime','-40 days'),5,4)||substr(date('now','localtime','-40 days'),1,4)),
        (substr(date('now','localtime','-30 days'),9,2)||substr(date('now','localtime','-30 days'),5,4)||substr(date('now','localtime','-30 days'),1,4)),
        (substr(date('now','localtime','-20 days'),9,2)||substr(date('now','localtime','-20 days'),5,4)||substr(date('now','localtime','-20 days'),1,4)),
        (substr(date('now','localtime','-10 days'),9,2)||substr(date('now','localtime','-10 days'),5,4)||substr(date('now','localtime','-10 days'),1,4))
    ;
    -- Result 1 (the data as loaded)
    SELECT * FROM table_data; 
    
    -- Result2 Data extracted using BETWEEN
    SELECT * FROM table_data 
        WHERE 
        substr(date,7,4)||substr(date,3,4)||substr(date,1,2) 
        BETWEEN date('now','localtime','-30 days') AND date('now','localtime')
    ;
    
    -- Result 3 data from 30 days ago on
    SELECT * FROM table_data WHERE 
        substr(date,7,4)||substr(date,3,4)||substr(date,1,2) >= date('now','localtime','-30 days')
    ;
    
    结果:- 结果1

    • (第3、4和5行,共30天)
    结果2

    结果3


    虽然方案2有更多的时间和空间。建议选项1是更好的选项,因为它将减少不必要的复杂性并提高效率。

    日期列中存储的值的格式是什么?我已经提到了它的“dd-MM-yyyy”@WaqasFarooq将存储日期的格式更改为“yyyy-MM-dd”然后您的SELECT语句将起作用。您还需要使用
    date()
    而不是
    datetime()
    ,然后像@forpas所说的那样修复存储的日期。比较表达式的两边都需要使用相同的格式。不过,就我个人而言,我不会使用日期和时间的单独列,而是使用一个,可能使用Unix秒而不是字符串,因为它存储起来更小,比较起来更快。
    DROP TABLE IF EXISTS table_data;
    CREATE TABLE IF NOT EXISTS table_data (id INTEGER PRIMARY KEY, date TEXT);
    INSERT INTO table_data (date) VALUES
      (substr(date('now','localtime','-50 days'),9,2)||substr(date('now','localtime','-50 days'),5,4)||substr(date('now','localtime','-50 days'),1,4)),
        (substr(date('now','localtime','-40 days'),9,2)||substr(date('now','localtime','-40 days'),5,4)||substr(date('now','localtime','-40 days'),1,4)),
        (substr(date('now','localtime','-30 days'),9,2)||substr(date('now','localtime','-30 days'),5,4)||substr(date('now','localtime','-30 days'),1,4)),
        (substr(date('now','localtime','-20 days'),9,2)||substr(date('now','localtime','-20 days'),5,4)||substr(date('now','localtime','-20 days'),1,4)),
        (substr(date('now','localtime','-10 days'),9,2)||substr(date('now','localtime','-10 days'),5,4)||substr(date('now','localtime','-10 days'),1,4))
    ;
    -- Result 1 (the data as loaded)
    SELECT * FROM table_data; 
    
    -- Result2 Data extracted using BETWEEN
    SELECT * FROM table_data 
        WHERE 
        substr(date,7,4)||substr(date,3,4)||substr(date,1,2) 
        BETWEEN date('now','localtime','-30 days') AND date('now','localtime')
    ;
    
    -- Result 3 data from 30 days ago on
    SELECT * FROM table_data WHERE 
        substr(date,7,4)||substr(date,3,4)||substr(date,1,2) >= date('now','localtime','-30 days')
    ;