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选择今天和前一天的所有记录_Android_Sqlite_Date_Select - Fatal编程技术网

Android SQLite选择今天和前一天的所有记录

Android SQLite选择今天和前一天的所有记录,android,sqlite,date,select,Android,Sqlite,Date,Select,我的数据库中有下表: database.execSQL("create table " + TABLE_LOGS + " (" + COLUMN_ID + " integer primary key autoincrement," + COLUMN_ID_DAY_EXERCISE + " integer not null," + COLUMN_REPS + " integer not null,"

我的数据库中有下表:

    database.execSQL("create table " + TABLE_LOGS + " (" 
            + COLUMN_ID + " integer primary key autoincrement," 
            + COLUMN_ID_DAY_EXERCISE + " integer not null,"
            + COLUMN_REPS + " integer not null,"
            + COLUMN_WEIGHT + " real not null,"
            + COLUMN_1RM + " real not null,"
            + COLUMN_DATE + " integer not null"
            + ")");
我将unix时间戳存储在列\u DATE字段integer中

现在,我有了以下函数,可以获取所有记录:

public Cursor fetchCurrentLogs(long dayExerciseDataID) {
    // where day = today
    Cursor cursor = database.rawQuery("select " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_REPS + ", " + MySQLiteHelper.COLUMN_WEIGHT + " " +
            "from " + MySQLiteHelper.TABLE_LOGS + " " +
            "where " + MySQLiteHelper.COLUMN_ID_DAY_EXERCISE + " = '" + dayExerciseDataID + "'", null);
    if (cursor != null) { cursor.moveToFirst(); }
    return cursor;
}
现在我想做的是,我想让这个函数只抓取今天的记录

然后我想做另一个完全相同的函数,但是我不想获取今天的记录,而是希望它获取前一天的记录。我所说的前一天,是指最近有记录的一天,而不是今天。所以可能是昨天,或者3天前,或者一个月前

我知道在MySQL中,你可以在今天做类似的事情:

其中date_formatfrom_unixtimeCOLUMN_date,“%Y-%m-%d”=date_formatnow,“%Y-%m-%d”

对于SQLite,与此等效的是什么

另外,有人能帮我解决上面提到的前一天的where条款吗


非常感谢。

不要在SQL语句中进行转换。在Java方法中进行转换。让Java方法创建完整的SQL语句。您所需要的只是一个将Java DateTime转换为unix格式的函数

Cursor cursor = database.rawQuery("select " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_REPS + ", " + MySQLiteHelper.COLUMN_WEIGHT + " " +
            "from " + MySQLiteHelper.TABLE_LOGS + " " +
            "where " + MySQLiteHelper.COLUMN_ID_DAY_EXERCISE + " = '" + dayExerciseDataID + 
            " OR " + MySQLiteHelper.COLUMN_ID_DAY_EXERCISE + " = '" + previousDay(dayExerciseDataID) +
"'", null);
您只需要创建上一天的方法

String sql = "SELECT * FROM myTable WHERE myDate >= date('now','-1 day')"; 
Cursor mycursor = db.rawQuery(sql);
编辑:

见:


也许为时已晚,但它可以帮助某人。我和以前的开发人员没有什么不同,我只是想把我的版本,可能它甚至不是最好的

    String selectQuery = "SELECT * FROM "+Your_table+" 
WHERE ("+Column_1+" = "+Value_For_Column1+" AND date("+Your_Column_With_Time_Values+") == date('now')) AND 

"+Some_Other_Column+" = "+Some_Other_Value+" OR "+Some_Other_Column+" = "+Some_Other_Value+" ORDER BY "+Your_Column+" DESC";
如果可能有一些错误,很容易是由于字符串连接造成的。
谢谢

我只需要选择前一天的记录,而不是所有前几天的记录。知道我的意思吗?假设我在数据库中有今天、3天前和6天前的条目,我想要一个函数获取今天的记录,另一个函数获取3天前的记录。嗨,我需要获取当前日期最后5分钟的记录,那么我如何才能实现这一点@Yaqub列中的值是多少?这是unix时间戳吗?
SELECT *
FROM MyTable
WHERE myDate >= date('now', '-1 days')
  AND myDate <  date('now')
    String selectQuery = "SELECT * FROM "+Your_table+" 
WHERE ("+Column_1+" = "+Value_For_Column1+" AND date("+Your_Column_With_Time_Values+") == date('now')) AND 

"+Some_Other_Column+" = "+Some_Other_Value+" OR "+Some_Other_Column+" = "+Some_Other_Value+" ORDER BY "+Your_Column+" DESC";