-
Hi there, I'm hoping to allow users to restore/upload a sqflite database file to my app. Of course my app requires a specific database format/schema, hence I was wondering if it is possible to check/validate an uploaded file has the correct schema for my app? My only thought so far is to compare an uploaded file to the required schema using something like Many thanks for any help, and for this amazing lib! :) [Originally asked: https://github.com//issues/849] |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Querying (I've close the bug as I think it is more appropriate here, thanks for asking) |
Beta Was this translation helpful? Give feedback.
-
Thanks very much for the information @alextekartik, this gave me the confidence to explore this further - the following seems to be working quite nicely: class SqliteMasterFields {
static const String type = 'type';
static const String name = 'name';
static const String tbl_name = 'tbl_name';
static const String rootpage = 'rootpage';
static const String sql = 'sql';
}
class SqliteMaster {
final String type;
final String name;
final String tbl_name;
final int rootpage;
final String sql;
const SqliteMaster({
required this.type,
required this.name,
required this.tbl_name,
required this.rootpage,
required this.sql,
});
static SqliteMaster fromJson(Map<String, Object?> json) => SqliteMaster(
type: json[SqliteMasterFields.type] as String,
name: json[SqliteMasterFields.name] as String,
tbl_name: json[SqliteMasterFields.tbl_name] as String,
rootpage: json[SqliteMasterFields.rootpage] as int,
sql: json[SqliteMasterFields.sql] as String,
);
static const expectedSql = {
"android_metadata": "CREATE TABLE android_metadata (locale TEXT)",
"sqlite_sequence": "CREATE TABLE sqlite_sequence(name,seq)",
"my_cool_table": "CREATE TABLE...",
};
static bool validate(List<SqliteMaster> tables) {
if (tables.length != 3) {
return false;
}
for (SqliteMaster table in tables) {
if (expectedSql[table.name] != table.sql) {
return false;
}
}
return true;
}
} And then: ...
isValid = SqliteMaster.validate((await db.query("sqlite_master"))
.map((json) => SqliteMaster.fromJson(json))
.toList());
... |
Beta Was this translation helpful? Give feedback.
Thanks very much for the information @alextekartik, this gave me the confidence to explore this further - the following seems to be working quite nicely: