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

时间:2018-03-07 05:28:24

标签: android sqlite nativescript angular2-nativescript

我尝试export整个sqlite database使用memory button进入SD卡click进行项目(备份功能)。我用google搜索同样的东西,但似乎是否定的反应。我正在使用https://github.com/NathanaelA/nativescript-sqlite plugin。还需要使用文件选择器导入exported database。现在,我正试图使用​​带有nativescript的java代码来访问它。任何nativescript方式的帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:1)

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

<强> EDITED

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