如何在nativescript/angular android中导入/导出sqlite数据库



我正在尝试将整个sqlite database export到带有项目button click的SD卡memory(备份功能(。我在谷歌上搜索了相同的内容,但似乎做出了负面反应。我为此使用 https://github.com/NathanaelA/nativescript-sqlite plugin。还需要使用文件选取器导入exported database。现在,我正在尝试使用带有nativescript的java代码来访问它。任何nativescript方式的帮助将不胜感激。提前谢谢。

我为

Android导出数据库的解决方案是创建一个开发人员页面并在其中添加一个按钮:

编辑

<Button class="btn  m-3" height="50" row="1" text="ExportDB" (tap)="exportDb()"></Button>

tap功能代码为:

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();
        }
    }

最新更新