如何从 flutter sqflite 中获取特定的行数?
How to get a specific row count from flutter sqflite?
我使用 flutter
sqflite
包创建了一个本地数据库来存储我的任务管理器应用程序 tasks
。现在我想计算任务总数,特别是 done tasks
.
的计数
总结:
我想在 UI 中向用户展示您已经完成了多少任务以及您还需要完成多少任务。为此,我需要我提到的特定计数。
任务模态class
class Task implements Comparable {
final int id;
final String taskTitle;
bool isDone;
Task({
required this.id,
required this.taskTitle,
required this.isDone,
});
Task.fromRow(Map<String, Object?> row)
: id = row['id'] as int,
taskTitle = row['taskTitle'] as String,
isDone = (row['isDone'] as int) == 1 ? true : false;
@override
String toString() =>
'Task:: id = $id , Title = $taskTitle , is Done = $isDone';
@override
int compareTo(covariant Task other) => other.id.compareTo(id);
}
数据库函数class
class TaskDatabase {
final String dbName;
Database? _db;
List<Task> _tasksList = [];
final _streamController = StreamController<List<Task>>.broadcast();
TaskDatabase({required this.dbName});
Stream<List<Task>> all() =>
_streamController.stream.map((tasks) => tasks..sort());
// opening database function
// closing database function
// fetching database data function
// deleting database data and updating etc...
}
我正在使用 List<Task>
来 cache
从数据库中获取的数据,并使用 streamBuilder
从该列表数据中呈现 UI。
数据库函数中存在的示例数据库函数class(Open DB Func)
Future<bool> open() async {
if (_db != null) {
return true;
}
final directory = await getApplicationDocumentsDirectory();
final path = '${directory.path}/$dbName';
try {
final db = await openDatabase(path);
_db = db;
//creating the database table using sqflite
const createTable = '''CREATE TABLE IF NOT EXISTS "TABLEOFTASKS" (
"id" INTEGER NOT NULL,
"taskTitle" TEXT,
"isDone" INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY("id" AUTOINCREMENT));''';
await db.execute(createTable);
// read all existing task objects from the db
_tasksList = await _fetchTasks();
_streamController.add(_tasksList);
return true;
} catch (e) {
print('error = $e');
return false;
}
}
尝试使用它来计算所有任务
var count = await database.rawQuery('SELECT COUNT(*) FROM TABLEOFTASKS');
并使用它来完成所有已完成的任务
var tasksDone = await database.rawQuery('SELECT COUNT(*), TABLEOFTASKS.isDone
FROM TABLEOFTASKS WHERE TABLEOFTASKS.isDone = 1');
我不确定他们是否以这种方式处理 flutter,但我在 MySql 命令行中使用此查询并且它工作正常
我使用 flutter
sqflite
包创建了一个本地数据库来存储我的任务管理器应用程序 tasks
。现在我想计算任务总数,特别是 done tasks
.
总结:
我想在 UI 中向用户展示您已经完成了多少任务以及您还需要完成多少任务。为此,我需要我提到的特定计数。
任务模态class
class Task implements Comparable {
final int id;
final String taskTitle;
bool isDone;
Task({
required this.id,
required this.taskTitle,
required this.isDone,
});
Task.fromRow(Map<String, Object?> row)
: id = row['id'] as int,
taskTitle = row['taskTitle'] as String,
isDone = (row['isDone'] as int) == 1 ? true : false;
@override
String toString() =>
'Task:: id = $id , Title = $taskTitle , is Done = $isDone';
@override
int compareTo(covariant Task other) => other.id.compareTo(id);
}
数据库函数class
class TaskDatabase {
final String dbName;
Database? _db;
List<Task> _tasksList = [];
final _streamController = StreamController<List<Task>>.broadcast();
TaskDatabase({required this.dbName});
Stream<List<Task>> all() =>
_streamController.stream.map((tasks) => tasks..sort());
// opening database function
// closing database function
// fetching database data function
// deleting database data and updating etc...
}
我正在使用 List<Task>
来 cache
从数据库中获取的数据,并使用 streamBuilder
从该列表数据中呈现 UI。
数据库函数中存在的示例数据库函数class(Open DB Func)
Future<bool> open() async {
if (_db != null) {
return true;
}
final directory = await getApplicationDocumentsDirectory();
final path = '${directory.path}/$dbName';
try {
final db = await openDatabase(path);
_db = db;
//creating the database table using sqflite
const createTable = '''CREATE TABLE IF NOT EXISTS "TABLEOFTASKS" (
"id" INTEGER NOT NULL,
"taskTitle" TEXT,
"isDone" INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY("id" AUTOINCREMENT));''';
await db.execute(createTable);
// read all existing task objects from the db
_tasksList = await _fetchTasks();
_streamController.add(_tasksList);
return true;
} catch (e) {
print('error = $e');
return false;
}
}
尝试使用它来计算所有任务
var count = await database.rawQuery('SELECT COUNT(*) FROM TABLEOFTASKS');
并使用它来完成所有已完成的任务
var tasksDone = await database.rawQuery('SELECT COUNT(*), TABLEOFTASKS.isDone
FROM TABLEOFTASKS WHERE TABLEOFTASKS.isDone = 1');
我不确定他们是否以这种方式处理 flutter,但我在 MySql 命令行中使用此查询并且它工作正常