statistics_db.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import 'dart:async';
  2. import 'package:path/path.dart';
  3. import 'package:sport/pages/run/location.dart';
  4. import 'package:sport/pages/web/statistics.dart';
  5. import 'package:sqflite/sqflite.dart';
  6. class StatisticsDB {
  7. static final StatisticsDB _instance = new StatisticsDB.internal();
  8. factory StatisticsDB() => _instance;
  9. static Database? _db;
  10. StatisticsDB.internal();
  11. Future<Database> get db async {
  12. if (_db != null) {
  13. return _db!;
  14. }
  15. _db = await initDb();
  16. return _db!;
  17. }
  18. static const String TABLE = 'Statistics';
  19. initDb() async {
  20. String databasesPath = await getDatabasesPath();
  21. String path = join(databasesPath, 'Statistics.db');
  22. var db = await openDatabase(path, version: 2, onCreate: _onCreate, onUpgrade: _onUpgrade);
  23. return db;
  24. }
  25. int? rid;
  26. FutureOr<void> _onCreate(Database db, int version) async {
  27. await db.execute("CREATE TABLE $TABLE(" +
  28. "id INTEGER PRIMARY KEY, game_id VARCHAR, step INTEGER, distance INTEGER," +
  29. "jump INTEGER, squat INTEGER, start_time INTEGER, end_time INTEGER, " + // [0 - 7]
  30. "level INTEGER, score REAL, record INTEGER, mode INTEGER, opponent_id INTEGER" +
  31. ", status INTEGER" +
  32. ", data VARCHAR" +
  33. ")");
  34. }
  35. FutureOr<void> _onUpgrade(Database db, int oldVersion, int newVersion) async {
  36. if (newVersion > oldVersion) {
  37. print("TrackDB _onUpgrade $oldVersion -> $newVersion");
  38. try {
  39. await db.execute("drop table $TABLE");
  40. } catch (e) {
  41. print(e);
  42. }
  43. }
  44. await _onCreate(db, newVersion);
  45. }
  46. //
  47. // Future<int> insert(Statistics item) async {
  48. // var dbClient = await db;
  49. // var id = await dbClient.insert(TABLE, {
  50. // "rid": rid,
  51. // "time": item.time,
  52. // "latitude": item.latitude,
  53. // "longitude": item.longitude,
  54. // "speed": item.speed,
  55. // "altitude": item.altitude,
  56. // "bearing": item.bearing,
  57. // "accuracy": item.accuracy,
  58. // "step": item.step,
  59. // "state": item.state,
  60. // });
  61. // return id;
  62. // }
  63. //
  64. Future<int> save(Statistics statistics) async {
  65. var dbClient = await db;
  66. var id = await dbClient.insert(TABLE, statistics.toJson());
  67. return id;
  68. }
  69. update(Statistics statistics) async {
  70. var dbClient = await db;
  71. var result = await dbClient.update(TABLE, statistics.toJson(), where: "id = ?", whereArgs: [statistics.id]);
  72. }
  73. Future<int> deleteRecord(int id) async {
  74. var dbClient = await db;
  75. await dbClient.delete(TABLE, where: "id = ?", whereArgs: [id]);
  76. return 1;
  77. }
  78. Future<List<Map<String, dynamic>>> find() async {
  79. var dbClient = await db;
  80. return await dbClient.rawQuery(
  81. 'SELECT * FROM $TABLE');
  82. }
  83. }