使用Sqlite Flutter在查询之间返回最近2天的记录

问题描述 投票:0回答:1

我希望你们能够帮助我!

我当前的代码无效。仅显示一条错误消息,提示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查询以返回最近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,`

感谢您的帮助!

sqlite flutter dart between sqflite
1个回答
0
投票

正如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''');

您需要在三重单引号之前再加上一个单引号。

© www.soinside.com 2019 - 2024. All rights reserved.