如何在 nativescript/angular android 中导入/导出 sqlite 数据库? [英] How import/export sqlite database in nativescript/angular android?

查看:34
本文介绍了如何在 nativescript/angular android 中导入/导出 sqlite 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 button export 将整个 sqlite 数据库 导入到 SD 卡 memory>单击 项目(备份功能).我在谷歌上搜索了同样的内容,但似乎是负面回应.我正在使用 https://github.com/NathanaelA/nativescript-sqlite 插件为此.还需要使用文件选择器导入导出的数据库.现在,我正在尝试使用带有 nativescript 的 java 代码来实现它.任何 nativescript 方式帮助将不胜感激.提前致谢.

I'm trying to export the whole sqlite database into sd card memory with a button click for a project (Backup feature). I googled for the same, but seems negative response. I'm using https://github.com/NathanaelA/nativescript-sqlite plugin for this. Also need to import the exported database with file picker. Right now, I'm trying to reach it using java code with nativescript. Any nativescript way help will be appreciated. Thanks in advance.

推荐答案

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

my solution to export database for android was to create a developer page and add a button in it :

已编辑

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

这篇关于如何在 nativescript/angular android 中导入/导出 sqlite 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆