1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import 'dart:async';
- import 'package:path/path.dart';
- import 'package:sport/bean/step_realtime_entity.dart';
- import 'package:sport/pages/run/location.dart';
- import 'package:sport/pages/web/statistics.dart';
- import 'package:sqflite/sqflite.dart';
- class StepRealtimeDB {
- static final StepRealtimeDB _instance = new StepRealtimeDB.internal();
- factory StepRealtimeDB() => _instance;
- static Database? _db;
- StepRealtimeDB.internal();
- Future<Database> get db async {
- if (_db != null) {
- return _db!;
- }
- _db = await initDb();
- return _db!;
- }
- static const String TABLE = 'step';
- initDb() async {
- String databasesPath = await getDatabasesPath();
- String path = join(databasesPath, 'step_realtime.db');
- var db = await openDatabase(path, version: 5, onCreate: _onCreate, onUpgrade: _onUpgrade);
- return db;
- }
- int? rid;
- FutureOr<void> _onCreate(Database db, int version) async {
- await db.execute("CREATE TABLE $TABLE(" +
- "id INTEGER PRIMARY KEY, user_id INTEGER, step INTEGER,motion INTEGER, type INTEGER, target INTEGER," +
- "start INTEGER, end INTEGER, data TEXT"
- ")");
- }
- FutureOr<void> _onUpgrade(Database db, int oldVersion, int newVersion) async {
- if (newVersion > oldVersion) {
- try {
- await db.execute("drop table $TABLE");
- } catch (e) {
- print(e);
- }
- }
- await _onCreate(db, newVersion);
- }
- Future<int> save(StepRealtimeEntity entity) async {
- var dbClient = await db;
- var id = await dbClient.insert(TABLE, entity.toJson());
- return id;
- }
- Future<int> delete(int id) async {
- var dbClient = await db;
- await dbClient.delete(TABLE, where: "id = ?", whereArgs: [id]);
- return 1;
- }
- Future<int> deleteAll(int userId) async {
- var dbClient = await db;
- await dbClient.delete(TABLE, where: "user_id = ?", whereArgs: [userId]);
- return 1;
- }
- Future<List<StepRealtimeEntity>> find(int userId, int type) async {
- var dbClient = await db;
- return (await dbClient.rawQuery('SELECT * FROM $TABLE WHERE user_id = $userId and motion = $type ORDER BY ID DESC')).map((e) => StepRealtimeEntity.fromJson(e)).toList();
- }
- }
|