DataBase.dart 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import 'package:path/path.dart';
  2. import 'package:sqflite/sqflite.dart' as Sqlite;
  3. import 'package:sqflite/sqflite.dart';
  4. import 'package:taskservice/Model/JXMemberModel.dart';
  5. import 'package:taskservice/Model/JXServiceModel.dart';
  6. class MyDataBase {
  7. static MyDataBase _instance;
  8. Database _database;
  9. String tableMembers = 'members';
  10. String tableServices = 'services';
  11. static Future<MyDataBase> get database async {
  12. if(_instance == null){
  13. _instance = MyDataBase();
  14. await _instance.init();
  15. }
  16. return _instance;
  17. }
  18. Future init() async {
  19. _database = await Sqlite.openDatabase(join((await Sqlite.getDatabasesPath()), 'data.db'));
  20. List<Map<String, dynamic>> result = await _database.rawQuery("CREATE TABLE IF NOT EXISTS $tableMembers(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)");
  21. result.toString();
  22. // _database.execute("CREATE TABLE $tableMembers(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)",);
  23. }
  24. Future<List<JXMemberModel>> queryAllMembers() async {
  25. List<Map<String , dynamic>> datas = await _database.query("$tableMembers");
  26. return datas.map((e) =>
  27. JXMemberModel()
  28. ).toList();
  29. }
  30. Future<void> saveMember(JXMemberModel jxMemberModel) async {
  31. await _database.insert(
  32. tableMembers,
  33. jxMemberModel.toMap(),
  34. conflictAlgorithm: ConflictAlgorithm.replace,
  35. );
  36. }
  37. Future<void> saveService(JXMemberModel jxMemberModel, JXServiceModel jxServiceModel) {
  38. }
  39. }