How to use External SQLite DB in Android

How to use External SQLite DB in Android

In this Post, I will explain how to import and use External SQLite DB in Android.
You can import and use SQLite database with the extensions like .db, .db3,sqlite and sqlite3.
The External DB is created with some desktop applications like SQLite Browser, SQLite Converter and so on.
After Generating the DB paste that into your App's assets folder.
Create a class named as DBHelper extending with SQLiteHelper. Paste the following code in that class.
public class DBHelper extends SQLiteOpenHelper {
    Context context;
    String DB_PATH;
    String divider = "/";
    String DB_NAME;

    public DBImporterExporter(Context context, String DB_NAME) {
        super(context, DB_NAME, null, 1);
        this.context = context;
        this.DB_NAME = DB_NAME;
        DB_PATH = divider + "data" + divider + "data" + divider + context.getPackageName() + divider + "databases/";

    public boolean isDataBaseExists() {
        File dbFile = new File(DB_PATH + DB_NAME);
        return dbFile.exists();

    public void importDataBaseFromAssets() throws IOException {


        InputStream myInput = context.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = > 0) {
            myOutput.write(buffer, 0, length);
        Toast.makeText(context.getApplicationContext(), "Successfully Imported", Toast.LENGTH_SHORT).show();
        // Close the streams


    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
The DB is Imported by using the following code
DBImporterExporter dbImporterExporter = new DBImporterExporter(getApplicationContext(), "external_db_android.sqlite");
 try {
    } catch (IOException e) {
To Check the Existence of DB use the following snippet in your class
Download Source Code

Having trouble while using this code or any doubt, comment me

Share this

Related Posts

Next Post »

Please Comment about the Posts and Blog