DataBase.dart 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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, "
  21. "name TEXT, tel TEXT, birth TEXT, prov TEXT, city TEXT, area TEXT, addr TEXT, remark TEXT)");
  22. }
  23. Future<List<JXMemberModel>> queryAllMembers() async {
  24. List<Map<String , dynamic>> data = await _database.query("$tableMembers");
  25. return data.map((e) =>
  26. JXMemberModel()..id = e['id']..name = e['name']..tel = e['tel']
  27. ).toList();
  28. }
  29. Future<void> saveMember(JXMemberModel jxMemberModel) async {
  30. await _database.insert(
  31. tableMembers,
  32. jxMemberModel.toMap(),
  33. conflictAlgorithm: ConflictAlgorithm.replace,
  34. );
  35. }
  36. Future<void> saveService(JXMemberModel jxMemberModel, JXServiceModel jxServiceModel) {
  37. }
  38. }