2

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.

1 Answer 1

1

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

EDITED

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

the tap function code is :

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();
        }
    }
Sign up to request clarification or add additional context in comments.

6 Comments

thanks a lot for your effort, but seems java code in between them, letme try and definitely will mark as correct if its working. Thanks again
to copy on ios and android you can use this : public copyFile(sourceFile: fs.File, destinationFile: fs.File) { const source = sourceFile.readSync((e) => { console.log(e); }); destinationFile.writeSync(source, (e) => { console.log(e); }); }
Thanks a lot for your answer, is there any method to import the exported file.?
you can use sqlitebrowser.org to open the it in your dev environnement if you want to import it in your app simply do the reverse copy the database from any source to this folder : "/data/data/{your package name }/databases/ "
hm, that will replace the existing scehma and data right ?, what if i have a table alteration for a new field.?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.