Browse Source

feat : sqlite int 类型 toMap,fromMap

dev
徐振升 1 week ago
parent
commit
cafa2a2412
  1. 2
      lib/data/models/operation.dart
  2. 4
      lib/data/models/problem_model.dart
  3. 43
      lib/data/providers/sqlite_provider.dart

2
lib/data/models/operation.dart

@ -12,7 +12,7 @@ enum Operation {
/// Operation
extension OperationExtension on Operation {
///
String get name {
String get displayName {
switch (this) {
case Operation.create:
return '创建';

4
lib/data/models/problem_model.dart

@ -93,7 +93,7 @@ class Problem {
'operation': operation.index, // operation
'censorTaskId': censorTaskId,
'bindData': bindData,
'isChecked': isChecked, // isChecked
'isChecked': isChecked ? 1 : 0, // isChecked
};
}
@ -117,7 +117,7 @@ class Problem {
: Operation.create, //
censorTaskId: map['censorTaskId'],
bindData: map['bindData'],
isChecked: map['isChecked'] as bool? ?? false, // isChecked
isChecked: (map['isChecked'] as int) == 1, // isChecked
);
}
}

43
lib/data/providers/sqlite_provider.dart

@ -13,6 +13,9 @@ class SQLiteProvider extends GetxService {
static const String _dbName = 'problems.db';
static const String _tableName = 'problems';
///
static const int _dbVersion = 1;
/// 访
late Database _database;
@ -23,21 +26,26 @@ class SQLiteProvider extends GetxService {
_initDatabase();
}
///
///
Future<void> _initDatabase() async {
try {
final databasePath = await getDatabasesPath();
final path = join(databasePath, _dbName);
_database = await openDatabase(path, version: 1, onCreate: _onCreate);
_database = await openDatabase(
path,
version: _dbVersion,
onCreate: _onCreate,
onUpgrade: _onUpgrade, //
);
} catch (e) {
// Get.log('数据库初始化失败:$e');
Get.log('数据库初始化失败:$e', isError: true);
rethrow;
}
}
///
/// **** `operation` `isChecked`
Future<void> _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE $_tableName(
@ -47,14 +55,29 @@ class SQLiteProvider extends GetxService {
imageUrls TEXT NOT NULL,
creationTime INTEGER NOT NULL,
syncStatus INTEGER NOT NULL,
operation INTEGER NOT NULL, --
operation INTEGER NOT NULL,
censorTaskId TEXT,
bindData TEXT,
isChecked INTEGER NOT NULL -- SQLite INTEGER
isChecked INTEGER NOT NULL
)
''');
}
///
///
Future<void> _onUpgrade(Database db, int oldVersion, int newVersion) async {
Get.log('正在将数据库从版本 $oldVersion 升级到 $newVersion...');
if (oldVersion < 2) {
// 1 2 'newColumn'
// await db.execute('ALTER TABLE $_tableName ADD COLUMN newColumn TEXT;');
// Get.log('已添加新列: newColumn');
}
// if (oldVersion < 3) {
// ...
// }
Get.log('数据库升级完成。');
}
// ---
/// ** (CRUD) **
@ -74,7 +97,7 @@ class SQLiteProvider extends GetxService {
conflictAlgorithm: ConflictAlgorithm.replace,
);
} catch (e) {
//
Get.log('插入问题失败:$e', isError: true);
return 0; // 0
}
}
@ -91,7 +114,7 @@ class SQLiteProvider extends GetxService {
whereArgs: [id],
);
} catch (e) {
//
Get.log('删除问题(ID: $id)失败:$e', isError: true);
return 0;
}
}
@ -109,7 +132,7 @@ class SQLiteProvider extends GetxService {
whereArgs: [problem.id],
);
} catch (e) {
//
Get.log('更新问题(ID: ${problem.id})失败:$e', isError: true);
return 0;
}
}
@ -131,7 +154,7 @@ class SQLiteProvider extends GetxService {
}
return null;
} catch (e) {
//
Get.log('获取问题(ID: $id)失败:$e', isError: true);
return null;
}
}
@ -181,7 +204,7 @@ class SQLiteProvider extends GetxService {
return maps.map((json) => Problem.fromMap(json)).toList();
} catch (e) {
//
Get.log('获取问题列表失败:$e', isError: true);
return [];
}
}

Loading…
Cancel
Save