DataBase.dart 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. String path = join((await Sqlite.getDatabasesPath()), 'data.db');
  15. print('dbPath:${path}');
  16. await _instance.init();
  17. }
  18. return _instance;
  19. }
  20. Future init() async {
  21. _database = await Sqlite.openDatabase(join((await Sqlite.getDatabasesPath()), 'data.db'));
  22. List<Map<String, dynamic>> result = await _database.rawQuery("CREATE TABLE IF NOT EXISTS $tableMembers(id INTEGER PRIMARY KEY,"
  23. "name TEXT, tel TEXT, birth TEXT, prov TEXT, city TEXT, area TEXT, addr TEXT, remark TEXT, services TEXT,"
  24. "createtime DATETIME DEFAULT CURRENT_TIMESTAMP,"
  25. "updatetime DATETIME DEFAULT CURRENT_TIMESTAMP);"
  26. "CREATE TRIGGER [updatetime]"
  27. "AFTER UPDATE"
  28. "ON members"
  29. "FOR EACH ROW"
  30. "BEGIN"
  31. "UPDATE members SET updatetime = CURRENT_TIMESTAMP WHERE id = old.id;"
  32. "END;");
  33. List<Map<String, dynamic>> services = await _database.rawQuery("CREATE TABLE IF NOT EXISTS $tableServices(id INTEGER PRIMARY KEY, "
  34. "kind TEXT, cycle INTEGER, cycleUnit TEXT, remindDay INTEGER, remark TEXT, "
  35. "createtime DATETIME DEFAULT CURRENT_TIMESTAMP, "
  36. "updatetime DATETIME DEFAULT CURRENT_TIMESTAMP);"
  37. "CREATE TRIGGER [updatetime]"
  38. "AFTER UPDATE"
  39. "ON members"
  40. "FOR EACH ROW"
  41. "BEGIN"
  42. "UPDATE members SET updatetime = CURRENT_TIMESTAMP WHERE id = old.id;"
  43. "END;");
  44. }
  45. Future<List<JXMemberModel>> queryAllMembers() async {
  46. List<Map<String , dynamic>> data = await _database.query("$tableMembers");
  47. return data.map((e) => JXMemberModel()
  48. ..id = e['id']
  49. ..name = e['name']
  50. ..tel = e['tel'])
  51. .toList();
  52. }
  53. Future<void> saveMember(JXMemberModel jxMemberModel) async {
  54. await _database.insert(
  55. tableMembers,
  56. jxMemberModel.toMap(),
  57. conflictAlgorithm: ConflictAlgorithm.replace,
  58. );
  59. }
  60. /*服务*/
  61. Future<List <JXServiceModel>> queryAllServices() async {
  62. List<Map<String , dynamic>> data = await _database.query("$tableServices");
  63. return data.map((e) => JXServiceModel()
  64. ..id = e['id']
  65. ..kind = e['kind']
  66. ..cycle = e['cycle']
  67. ..cycleUnit = e['cycleUnit']
  68. ..remindDay = e['remindDay']
  69. ..remark = e['remark'])
  70. .toList();
  71. }
  72. /*添加新服务*/
  73. Future<JXServiceModel> saveService(JXServiceModel jxServiceModel) async {
  74. await _database.insert(
  75. tableServices,
  76. jxServiceModel.toMap(),
  77. conflictAlgorithm: ConflictAlgorithm.replace,
  78. );
  79. return null;
  80. }
  81. }