Android - Export and Import SQLite

13:23 Pedro 0 Comments



As I said in this post, you can find the sqlite file of your application in /data/data/APP_PACKGE_NAME/databases/DATABASE_NAME and you can interact directly with this file. In this post I'll show how to export a created sqlite and how to import some sqlite into your application.

Here is the class that I use to do it:
import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class SQLiteTools {

    private String databaseName;
    private Context context;

    public SQLiteTools(Context context, String databaseName) {
        this.context = context;
        this.databaseName = databaseName;
    }

    public boolean exportDatabase(File to) {
        File from = this.getCurrentDatabaseFile();
        if(!to.getParentFile().exists())
            to.getParentFile().mkdirs();
        return execute(from, to);
    }

    public boolean importDatabase(File from) {
        File to = this.getCurrentDatabaseFile();
        return execute(from, to);
    }

    private File getCurrentDatabaseFile() {
        return new File(context.getDatabasePath(databaseName).getPath());
    }

    private boolean execute(File from, File to){
        try {
            copyFile(from, to);
            Log.i("SQLITETOOLS FROM/TO", from.getAbsolutePath() + "/" + to.getAbsolutePath());
            return true;
        } catch (IOException e) {
            Log.i("SQLITETOOLS ERROR", e.getMessage());
        }
        return false;
    }

    private void copyFile(File from, File to) throws IOException {
        FileInputStream in = new FileInputStream(from);
        FileOutputStream out = new FileOutputStream(to);
        FileChannel fromChannel = null, toChannel = null;
        try {
            fromChannel = in.getChannel();
            toChannel = out.getChannel();
            fromChannel.transferTo(0, fromChannel.size(), toChannel);
        } finally {
            if (fromChannel != null)
                fromChannel.close();
            if (toChannel != null)
                toChannel.close();
        }
    }

}
As you can see, there are no complex operations. All what the methods do are copy the sqlite file from your application (using getDatabasePath(databaseName).getPath() to get the path) to a determined location and the inverse operation.

Example of usage:
SQLiteTools sqliteTools = new SQLiteTools(context, "yourdbname");
sqliteTools.exportDatabase(new File("/path/to/export"));
sqliteTools.importDatabase(new File("/path/from/import"));
Remember to declare in your manifest if you are intended to export to external storage:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

You can download a full example in masterapp repository on my GitHub! Master App contains a lot of examples that could help you. For this post, take a look on sqlite package.

0 comentários: