使用 Sqlite flutter BETWEEN 查询 return 最近 2 天的记录

Using Sqlite flutter BETWEEN query to return last 2 days records

我希望你们能帮助我!

我当前的代码不起作用。刚刚显示一条错误消息说 Unhandled Exception: DatabaseException(unrecognized token: "'1582268587562" (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM my_table WHERE date BETWEEN '1582095787562' AND '1582268587562) sql 'SELECT * FROM my_table WHERE date BETWEEN '1582095787562' AND '1582268587562' args []}

我正在尝试让我的 sqflite 查询 return 最近 2 天的记录。这是我目前正在使用的查询:

Future<List<Map<String, dynamic>>> queryLastTwoDays() async {
    Database db = await instance.database;
    DateTime now = DateTime.now();
    DateTime twoDaysAgoFromNow = now.subtract(Duration(days: 2));
    var today = now.millisecondsSinceEpoch;
    var twoDaysAgo = twoDaysAgoFromNow.millisecondsSinceEpoch;
    return await db.rawQuery('''SELECT * FROM $table WHERE $columnDate BETWEEN '$twoDaysAgo' AND '$today''');
  }

数据库结构

CREATE TABLE $table ( $columnId INTEGER PRIMARY KEY, $columnName TEXT NOT NULL, $columnAge INTEGER NOT NULL, $columnColour TEXT NOT NULL, $columnDate INTEGER NOT NULL )

用于插入的示例数据:

 `DatabaseHelper.columnName : 'Breakfast',
  DatabaseHelper.columnAge  : 23,
  DatabaseHelper.columnColour : 'red',
  DatabaseHelper.columnDate : DateTime.now().millisecondsSinceEpoch,`

感谢您的帮助!

正如评论中 Shawn 指出的那样,您生成的 SQL 缺少结束引号。查看错误信息:

...while compiling: SELECT * FROM my_table 
WHERE date BETWEEN '1582095787562' AND '1582268587562)

括号前没有结束'

是从这条线来的,我觉得:

    return await db.rawQuery('''SELECT * FROM $table WHERE $columnDate BETWEEN '$twoDaysAgo' AND '$today''');

三重单引号前还需要一个单引号。