在我的Ionic 2应用程序中,我希望有一个备份功能。我有一个包含大量数据的SQLite数据库。此数据应再次与App进行比较。我的想法是,将这些数据作为SQL导出uk.co.workingedge.cordova.plugin.sqliteporter并将此sql保存到文件(Cordova writeFile)。 import函数加载文件(File.readAsText)并使用importSqlToDb再次将数据写入DB。
点击按钮:
// trigger export
this.dbService.dumpDatabase();
// trigger import
this.dbService.importDatabase();
DBService:
public dumpDatabase() {
let _self = this;
(<any>window).cordova.plugins.sqlitePorter.exportDbToSql(this.database, {
successFn: function (sql, count) {
if (count > 0) {
_self.fileService.exportSQLToFile(sql);
} else {
// show message
}
},
dataOnly: true
});
}
public importDatabase() {
this.fileService.importSQLFromFile().then((sql) => {
(<any>window).cordova.plugins.sqlitePorter.importSqlToDb(this.database, sql, {
successFn: function (count) {
alert("Successfully imported " + count + " SQL statements to DB");
},
errorFn: function (error) {
alert("The following error occurred: " + error.message);
},
progressFn: function (current, total) {
console.log("Imported " + current + "/" + total + " statements");
}
});
}, (message) => {
[...]
});
}
的FileService:
exportSQLToFile(sql) {
let fs = cordova.file.externalDataDirectory;
let fileName = "Dump.sql";
File.writeFile(fs, fileName, sql, {replace: true}).then((success) => {
[...]
}, (error) => {
[...]
});
}
importSQLFromFile(): Promise<any> {
let fs = cordova.file.externalDataDirectory;
let fileName = "Dump.sql";
return new Promise((resolve, reject) => {
// check if backup exists
File.checkFile(fs, fileName).then(() => {
return File.readAsText(fs, fileName).then((result) => {
if(typeof result == "string") {
resolve(result);
} else {
reject(result);
}
}).catch((error) => {
reject(error.message);
});
}).catch((error) => {
reject(error.message);
});
});
}
我想让用户控制文件位置,并且可以在还原时从不同的备份中进行选择。
到目前为止,是否有人实施了类似的内容,或者知道如何做得更好?