SqfliteDataBase.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter_habit/database/entity/BasicInfo.dart';
  3. import 'package:flutter_habit/database/entity/ExerciseInfo.dart';
  4. import 'package:flutter_habit/database/entity/FoodInfo.dart';
  5. import 'package:flutter_habit/database/entity/LifeInfo.dart';
  6. import 'package:flutter_habit/database/entity/ScheduledExercise.dart';
  7. import 'package:flutter_habit/database/entity/SportInfo.dart';
  8. import 'package:flutter_habit/database/entity/StudyInfo.dart';
  9. import 'package:sqflite/sqflite.dart';
  10. class SqfliteDataBase {
  11. static final String dbPath = "repository.db.sql";
  12. static Database? _database;
  13. static Future<void> init() async {
  14. if (_database == null || !_database!.isOpen) {
  15. String databasesPath = "${await getDatabasesPath()}/$dbPath";
  16. _database = await openDatabase(databasesPath, version: 1);
  17. await _createTablesIfExists();
  18. debugPrint("init SqfliteDataBase");
  19. }
  20. }
  21. static Database? getInstance() {
  22. return _database;
  23. }
  24. static Future<void> _createTablesIfExists() async {
  25. await BasicInfo.create();
  26. await LifeInfo.create();
  27. await FoodInfo.create();
  28. await ExerciseInfo.create();
  29. await SportInfo.create();
  30. await StudyInfo.create();
  31. await ScheduledExercise.create();
  32. }
  33. static Future<void> resetTables() async {
  34. await BasicInfo.recreate();
  35. await LifeInfo.recreate();
  36. await FoodInfo.recreate();
  37. await ExerciseInfo.recreate();
  38. await SportInfo.recreate();
  39. await StudyInfo.recreate();
  40. await ScheduledExercise.recreate();
  41. }
  42. }