import/exportsqlite数据库如何在nativescript/angularandroid?
How import/export sqlite database in nativescript/angular android?
我正在尝试 export
将整个 sqlite database
放入 sd 卡 memory
中,并带有 button
click
项目(备份功能)。我在谷歌上搜索了同样的内容,但似乎是负面的回应。为此,我使用 https://github.com/NathanaelA/nativescript-sqlite plugin
。还需要使用文件选择器导入 exported database
。现在,我正在尝试使用 java 代码和 nativescript
来实现它。任何 nativescript
方式的帮助将不胜感激。提前致谢。
我为 android 导出数据库的解决方案是创建一个开发者页面并在其中添加一个按钮:
已编辑
<Button class="btn m-3" height="50" row="1" text="ExportDB" (tap)="exportDb()"></Button>
点击功能代码为:
public exportDb() {
console.log('######################### exporting DB ##################');
this.copyAndroidFileUsingStream('/data/data/{your package name }/databases/{database name}.db',
fs.path.join(this.getDownloadFolderPath(), {output filename}.db));
}
public getDownloadFolderPath() {
if (!!application.ios) {
const fileManager = utilModule.ios.getter(NSFileManager, NSFileManager.defaultManager);
const paths = fileManager.URLsForDirectoryInDomains(15, 1);
const url = paths.objectAtIndex(0);
return url.path;
} else {
return android.os.Environment.getExternalStoragePublicDirectory(
android.os.Environment.DIRECTORY_DOWNLOADS).toString();
}
}
// copy file only for android
public copyAndroidFileUsingStream = (source, dest): void => {
let is: java.io.InputStream = null;
let os: java.io.OutputStream = null;
try {
is = new java.io.FileInputStream(source);
os = new java.io.FileOutputStream(dest);
// let buffer = Array.create("byte", 1024);
const buffer = Array.create('byte', 4096);
let length: number;
// tslint:disable-next-line:no-conditional-assignment
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} catch (e) {
this.errorService.handleError(e);
} finally {
console.log('copy done');
is.close();
os.close();
}
}
我正在尝试 export
将整个 sqlite database
放入 sd 卡 memory
中,并带有 button
click
项目(备份功能)。我在谷歌上搜索了同样的内容,但似乎是负面的回应。为此,我使用 https://github.com/NathanaelA/nativescript-sqlite plugin
。还需要使用文件选择器导入 exported database
。现在,我正在尝试使用 java 代码和 nativescript
来实现它。任何 nativescript
方式的帮助将不胜感激。提前致谢。
我为 android 导出数据库的解决方案是创建一个开发者页面并在其中添加一个按钮:
已编辑
<Button class="btn m-3" height="50" row="1" text="ExportDB" (tap)="exportDb()"></Button>
点击功能代码为:
public exportDb() {
console.log('######################### exporting DB ##################');
this.copyAndroidFileUsingStream('/data/data/{your package name }/databases/{database name}.db',
fs.path.join(this.getDownloadFolderPath(), {output filename}.db));
}
public getDownloadFolderPath() {
if (!!application.ios) {
const fileManager = utilModule.ios.getter(NSFileManager, NSFileManager.defaultManager);
const paths = fileManager.URLsForDirectoryInDomains(15, 1);
const url = paths.objectAtIndex(0);
return url.path;
} else {
return android.os.Environment.getExternalStoragePublicDirectory(
android.os.Environment.DIRECTORY_DOWNLOADS).toString();
}
}
// copy file only for android
public copyAndroidFileUsingStream = (source, dest): void => {
let is: java.io.InputStream = null;
let os: java.io.OutputStream = null;
try {
is = new java.io.FileInputStream(source);
os = new java.io.FileOutputStream(dest);
// let buffer = Array.create("byte", 1024);
const buffer = Array.create('byte', 4096);
let length: number;
// tslint:disable-next-line:no-conditional-assignment
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} catch (e) {
this.errorService.handleError(e);
} finally {
console.log('copy done');
is.close();
os.close();
}
}