import 'package:sqflite/sqflite.dart';
|
|
import 'package:sqflite_pust_local_notification/models/task_model.dart';
|
|
|
|
class DBHelper {
|
|
static Database? _db;
|
|
static final int _version = 1;
|
|
static final String _tableName = "tbtask";
|
|
static final String _DbName = "task.db";
|
|
|
|
static Future<void> initDb() async {
|
|
if (_db != null) {
|
|
return;
|
|
}
|
|
try {
|
|
String _path = await getDatabasesPath() + _DbName;
|
|
_db = await openDatabase(
|
|
_path,
|
|
version: _version,
|
|
onCreate: (db, version) {
|
|
return db.execute(
|
|
"CREATE TABLE $_tableName("
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
|
"title STRING, note TEXT, date STRING,"
|
|
"startTime STRING, endTime STRING,"
|
|
"remind INTEGER, repeat STRING,"
|
|
"color INTEGER, "
|
|
"isCompleted INTEGER)",
|
|
);
|
|
},
|
|
);
|
|
} catch (ex) {
|
|
print(ex);
|
|
}
|
|
}
|
|
|
|
static Future<int> insert(TaskModel? task) async {
|
|
return await _db?.insert(_tableName, task!.toJson()) ?? 1;
|
|
}
|
|
|
|
static Future<List<Map<String, dynamic>>> query() async {
|
|
return await _db!.query(_tableName);
|
|
}
|
|
|
|
static delete(TaskModel taskModel) async {
|
|
return await _db!
|
|
.delete(_tableName, where: 'id=?', whereArgs: [taskModel.id]);
|
|
}
|
|
|
|
static update(int id) async {
|
|
return await _db!.rawUpdate('''
|
|
UPDATE tbtask
|
|
SET isCompleted = ?
|
|
WHERE id = ?
|
|
''', [1, id]);
|
|
}
|
|
}
|