This is flutter sqlite local notification project. User can add task.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.4 KiB

2 years ago
2 years ago
  1. import 'package:sqflite/sqflite.dart';
  2. import 'package:sqflite_pust_local_notification/models/task_model.dart';
  3. class DBHelper {
  4. static Database? _db;
  5. static final int _version = 1;
  6. static final String _tableName = "tbtask";
  7. static final String _DbName = "task.db";
  8. static Future<void> initDb() async {
  9. if (_db != null) {
  10. return;
  11. }
  12. try {
  13. String _path = await getDatabasesPath() + _DbName;
  14. _db = await openDatabase(
  15. _path,
  16. version: _version,
  17. onCreate: (db, version) {
  18. return db.execute(
  19. "CREATE TABLE $_tableName("
  20. "id INTEGER PRIMARY KEY AUTOINCREMENT,"
  21. "title STRING, note TEXT, date STRING,"
  22. "startTime STRING, endTime STRING,"
  23. "remind INTEGER, repeat STRING,"
  24. "color INTEGER, "
  25. "isCompleted INTEGER)",
  26. );
  27. },
  28. );
  29. } catch (ex) {
  30. print(ex);
  31. }
  32. }
  33. static Future<int> insert(TaskModel? task) async {
  34. return await _db?.insert(_tableName, task!.toJson()) ?? 1;
  35. }
  36. static Future<List<Map<String, dynamic>>> query() async {
  37. return await _db!.query(_tableName);
  38. }
  39. static delete(TaskModel taskModel) async {
  40. return await _db!
  41. .delete(_tableName, where: 'id=?', whereArgs: [taskModel.id]);
  42. }
  43. static update(int id) async {
  44. return await _db!.rawUpdate('''
  45. UPDATE tbtask
  46. SET isCompleted = ?
  47. WHERE id = ?
  48. ''', [1, id]);
  49. }
  50. }