【问题标题】:How import/export sqlite database in nativescript/angular android?如何在 nativescript/angular android 中导入/导出 sqlite 数据库?
【发布时间】:2018-03-07 05:28:23
【问题描述】:

我正在尝试将 export 整个 sqlite database 放入 SD 卡 memorybutton click 用于项目(备份功能)。我用谷歌搜索了同样的东西,但似乎是负面的回应。我为此使用https://github.com/NathanaelA/nativescript-sqliteplugin。还需要使用文件选择器导入exported database。现在,我正在尝试使用带有nativescript 的java 代码来访问它。任何nativescript 方式的帮助将不胜感激。提前致谢。

【问题讨论】:

    标签: android sqlite nativescript angular2-nativescript


    【解决方案1】:

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

    【讨论】:

    • 非常感谢您的努力,但似乎它们之间存在 java 代码,让我尝试一下,如果它工作,肯定会标记为正确。再次感谢
    • 复制到 ios 和 android 你可以使用这个:public copyFile(sourceFile: fs.File, destinationFile: fs.File) { const source = sourceFile.readSync((e) =&gt; { console.log(e); }); destinationFile.writeSync(source, (e) =&gt; { console.log(e); }); }
    • 非常感谢您的回答,有什么方法可以导入导出的文件吗?
    • 你可以使用sqlitebrowser.org在你的开发环境中打开它,如果你想在你的应用程序中导入它,只需将数据库从任何源反向复制到这个文件夹:“/data/data /{你的包名}/databases/ "
    • hm,这将替换现有的 scehma 和数据吗?如果我有一个新字段的表更改。?
    猜你喜欢
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 2011-09-26
    • 2018-08-26
    • 1970-01-01
    相关资源
    最近更新 更多