123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import 'dart:async';
- import 'package:path/path.dart';
- import 'package:sport/provider/bluetooth.dart';
- import 'package:sqflite/sqflite.dart';
- class StepDB {
- static final StepDB _instance = new StepDB.internal();
- factory StepDB() => _instance;
- static Database _db;
- StepDB.internal();
- Future<Database> get db async {
- if (_db != null) {
- return _db;
- }
- _db = await initDb();
- return _db;
- }
- final String TABLE = 'step';
- initDb() async {
- String databasesPath = await getDatabasesPath();
- String path = join(databasesPath, 'step.db');
- var db = await openDatabase(path, version: 1, onCreate: _onCreate);
- return db;
- }
- FutureOr<void> _onCreate(Database db, int version) async {
- await db.execute('CREATE TABLE $TABLE(id INTEGER PRIMARY KEY, step INTEGER, distance INTEGER, time INTEGER)');
- }
- Future<int> insert(PartItem item) async {
- var dbClient = await db;
- var result = await dbClient.insert(TABLE, item.toJson());
- return result;
- }
- Future insertAll(List<PartItem> items) async {
- var dbClient = await db;
- var batch = dbClient.batch();
- items.forEach((item) {
- batch.insert(TABLE, item.toJson());
- });
- batch.commit();
- }
- Future<List<Map<String, dynamic>>> find() async {
- var dbClient = await db;
- return await dbClient.rawQuery('SELECT SUM(step) as st, SUM(distance) as di, time FROM $TABLE GROUP BY time ORDER BY time LIMIT 1000');
- }
- Future<int> delete(int time) async {
- var dbClient = await db;
- return await dbClient.delete(TABLE, where: "time <= ?", whereArgs: [time]);
- }
- }
|