Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import and Export #560

Open
deshario opened this issue Mar 24, 2019 · 1 comment
Open

Import and Export #560

deshario opened this issue Mar 24, 2019 · 1 comment

Comments

@deshario
Copy link

How to import and export "dbname.db" ?

  1. How to export db into storage ? (I had done manually)
  2. How to import db from storage to app ? (Not idea yet)
private void exportDBNow(String foldertoSave){
        File sd = new File(foldertoSave);
        boolean success = true;
        if (!sd.exists()) {
            success = sd.mkdir();
        }
        if (success) {
            File data = Environment.getDataDirectory();
            FileChannel source=null;
            FileChannel destination=null;
            String currentDBPath = "/data/"+ SettingsActivity.getInstance().getPackageName() +"/databases/"+ "agriculture.db";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, "agriculture.db");
            try {
                source = new FileInputStream(currentDB).getChannel();
                destination = new FileOutputStream(backupDB).getChannel();
                destination.transferFrom(source, 0, source.size());
                source.close();
                destination.close();
                Toast.makeText(this, "Please wait", Toast.LENGTH_SHORT).show();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    }



** I wanna know how to import .db file into app Thankx**
@15characterlimi
Copy link

15characterlimi commented Jul 12, 2021

Even your export code above does not work reliably, because databases consist of more than one file (at least from Android Pie onwards, which enabled write-ahead logging by default). You'll have to use sqlite commands to make sure your export is aware of latest sqlite features (such as write-ahead logging). One way to safely export a database is to run sqlite3's own .dump command (i.e. run /system/bin/sqlite3 /path/to/database .dump). That command's output is SQL that will recreate the database.

Obviously, you will also need to make sure that no concurrent access on the database is happening. It's best to close the database before dumping it, and make sure the database doesn't exist before restoring it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants