如何在 Flutter 中按月获取数据 sqflite?
How to get data month wise sqflite in Flutter?
Image for how does data look 我正在使用 flutter 制作一个费用跟踪器应用程序,我想显示用户每个月完成的费用总和,例如:
Month Amount Spended
January 2000
February 1600
Database columns:
"CREATE TABLE $TABLE_EXPENSES ("
"$COLUMN_ID INTEGER PRIMARY KEY,"
"$COLUMN_NAME TEXT,"
"$COLUMN_AMOUNT TEXT,"
"$COLUMN_UNNECESSARYEXPENSES INTEGER,"
"$COLUMN_CATEGORY TEXT,"
"$COLUMN_DATETIME TEXT"
")",
我正在使用 sqflite 创建数据库并将数据存储为文本。我想检索一个月中每一天的费用,然后汇总每一天的费用并显示在 ListTile
编辑:
查询:'SELECT * SUM(COLUMN_AMOUNT) FROM TABLE_EXPENSES WHERE COLUMN_DATETIME >= ? AND COLUMN_DATETIME <= ?'
这是我得到的错误(用于查询):
E/SQLiteLog(10318): (1) near "SELECT": syntax error
E/flutter (10318): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: DatabaseException(near "SELECT": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM expenses WHERE SELECT * SUM(COLUMN_AMOUNT) FROM TABLE_EXPENSES WHERE COLUMN_DATETIME >= 2019 AND COLUMN_DATETIME <= 1989) sql 'SELECT * FROM expenses WHERE SELECT * SUM(COLUMN_AMOUNT) FROM TABLE_EXPENSES WHERE COLUMN_DATETIME >= ? AND COLUMN_DATETIME <= ?' args [2019, 1989]}
我目前使用的功能:
Future getDataJan() async{
final db = await database;
sumJan= db.rawQuery(
'SELECT SUM(AMOUNT) FROM EXPENSES WHERE DATETIME >= ? AND DATETIME <= ?',
[2021-01-01, 2021-01-31] ).then(Sqflite.firstIntValue);
finalJan=sumJan.toString();
}
图片:
errorImage
感谢您的回复。
您可以使用 yyyy-MM-dd HH:mm:SS
格式的日期,并在月份之间创建对数据库的搜索实体请求。
日期格式示例:
DateFormat('yyyy-MM-dd HH:mm:SS').format(date)
查询示例:
return database.query(
tableName,
where: 'date >= ? and date <= ?',
whereArgs: [from, to],
)
.then((data) => data.map(_fromMap).toList());
示例中的变量:
from
- 每月的第一天 (2020-10-01);
to
- 一个月的最后一天 (2020-10-31);
?
in where
- 从 whereArgs
获取值(第一个 ?
- from
,第二个 - to
);
注意:在评论中描述了您应该使用 SQFLite 工具来创建请求并且不要使用原始查询(出于性能原因)。
Select 例子
final result = await database.rawQuery(
'select * sum(COLUMN_AMOUNT) from TABLE_EXPENSES where COLUMN_DATETIME >= ? and COLUMN_DATETIME <= ?',
[from, to],
).then(Sqflite.firstIntValue);
Image for how does data look 我正在使用 flutter 制作一个费用跟踪器应用程序,我想显示用户每个月完成的费用总和,例如:
Month Amount Spended
January 2000
February 1600
Database columns:
"CREATE TABLE $TABLE_EXPENSES ("
"$COLUMN_ID INTEGER PRIMARY KEY,"
"$COLUMN_NAME TEXT,"
"$COLUMN_AMOUNT TEXT,"
"$COLUMN_UNNECESSARYEXPENSES INTEGER,"
"$COLUMN_CATEGORY TEXT,"
"$COLUMN_DATETIME TEXT"
")",
我正在使用 sqflite 创建数据库并将数据存储为文本。我想检索一个月中每一天的费用,然后汇总每一天的费用并显示在 ListTile
编辑:
查询:'SELECT * SUM(COLUMN_AMOUNT) FROM TABLE_EXPENSES WHERE COLUMN_DATETIME >= ? AND COLUMN_DATETIME <= ?'
这是我得到的错误(用于查询):
E/SQLiteLog(10318): (1) near "SELECT": syntax error
E/flutter (10318): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: DatabaseException(near "SELECT": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM expenses WHERE SELECT * SUM(COLUMN_AMOUNT) FROM TABLE_EXPENSES WHERE COLUMN_DATETIME >= 2019 AND COLUMN_DATETIME <= 1989) sql 'SELECT * FROM expenses WHERE SELECT * SUM(COLUMN_AMOUNT) FROM TABLE_EXPENSES WHERE COLUMN_DATETIME >= ? AND COLUMN_DATETIME <= ?' args [2019, 1989]}
我目前使用的功能:
Future getDataJan() async{
final db = await database;
sumJan= db.rawQuery(
'SELECT SUM(AMOUNT) FROM EXPENSES WHERE DATETIME >= ? AND DATETIME <= ?',
[2021-01-01, 2021-01-31] ).then(Sqflite.firstIntValue);
finalJan=sumJan.toString();
}
图片: errorImage
感谢您的回复。
您可以使用 yyyy-MM-dd HH:mm:SS
格式的日期,并在月份之间创建对数据库的搜索实体请求。
日期格式示例:
DateFormat('yyyy-MM-dd HH:mm:SS').format(date)
查询示例:
return database.query(
tableName,
where: 'date >= ? and date <= ?',
whereArgs: [from, to],
)
.then((data) => data.map(_fromMap).toList());
示例中的变量:
from
- 每月的第一天 (2020-10-01);to
- 一个月的最后一天 (2020-10-31);?
inwhere
- 从whereArgs
获取值(第一个?
-from
,第二个 -to
);
注意:在评论中描述了您应该使用 SQFLite 工具来创建请求并且不要使用原始查询(出于性能原因)。
Select 例子
final result = await database.rawQuery(
'select * sum(COLUMN_AMOUNT) from TABLE_EXPENSES where COLUMN_DATETIME >= ? and COLUMN_DATETIME <= ?',
[from, to],
).then(Sqflite.firstIntValue);