Skip to content Skip to sidebar Skip to footer

Copy SQLite Database From Assets Folder

I wanna copy SQLite database from assets folder. This my DatabaseAdapter.java class package com.example.dictionary; import java.io.FileOutputStream; import java.io.IOException; im

Solution 1:

put all this method in ur helper class.

public DataBaseHelper(Context context) {

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
    mContext = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

/**
 * This method will create database in application package /databases
 * directory when first time application launched
 **/
public void createDataBase() throws IOException {
    boolean mDataBaseExist = checkDataBase();
    if (!mDataBaseExist) {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException mIOException) {
            mIOException.printStackTrace();
            throw new Error("Error copying database");
        } finally {
            this.close();
        }
    }
}

/** This method checks whether database is exists or not **/
private boolean checkDataBase() {
    try {
        final String mPath = DATABASE_PATH + DATABASE_NAME;
        final File file = new File(mPath);
        if (file.exists())
            return true;
        else
            return false;
    } catch (SQLiteException e) {
        e.printStackTrace();
        return false;
    }
}

/**
 * This method will copy database from /assets directory to application
 * package /databases directory
 **/
private void copyDataBase() throws IOException {
    try {

        InputStream mInputStream = mContext.getAssets().open(DATABASE_NAME);
        String outFileName = DATABASE_PATH + DATABASE_NAME;
        OutputStream mOutputStream = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = mInputStream.read(buffer)) > 0) {
            mOutputStream.write(buffer, 0, length);
        }
        mOutputStream.flush();
        mOutputStream.close();
        mInputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/** This method open database for operations **/
public boolean openDataBase() throws SQLException {
    String mPath = DATABASE_PATH + DATABASE_NAME;
    myDataBase = SQLiteDatabase.openDatabase(mPath, null,
            SQLiteDatabase.OPEN_READWRITE);
    return myDataBase.isOpen();
}

/** This method close database connection and released occupied memory **/
@Override
public synchronized void close() {
    if (myDataBase != null)
        myDataBase.close();
    SQLiteDatabase.releaseMemory();
    super.close();
}

Solution 2:

Here is my working code..

void copyDataBase() throws IOException {

        String outFileName = DB_PATH + DB_NAME;

        OutputStream myOutput = new FileOutputStream(outFileName);

        byte[] buffer = new byte[1024];
        int length;

        InputStream myInput = myContext.getAssets().open("mydb.sqlite");
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myInput.close();

        myOutput.flush();
        myOutput.close();

    }

Solution 3:

DataBaseHelper myDbHelper = new DataBaseHelper();
      myDbHelper = new DataBaseHelper(this);
      try {
            myDbHelper.createDataBase();

      } catch (IOException ioe) {

            throw new Error("Unable to create database");
      }

      try {      
            myDbHelper.openDataBase();

      }catch(SQLException sqle){

            throw sqle;
      }

For Copying the database these method is useful

 private void copyDataBase() throws IOException
      {
            String outFileName = DATABASE_PATH + DATABASE_NAME;
            OutputStream myOutput = new FileOutputStream(outFileName);
            InputStream myInput = myContext.getAssets().open(DATABASE_NAME);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0)
            {
                  myOutput.write(buffer, 0, length);
            }
            myInput.close();
            myOutput.flush();
            myOutput.close();
      }

Reference from :http://androidtutorials60.blogspot.in/2013/03/1.html


Solution 4:

 public void CopyDatabase() throws IOException {
    openOrCreateDatabase("you_db_file.db", 0, null);
    InputStream mInput = getAssets().open("databases/forall.db");
    OutputStream mOutput = new FileOutputStream("/data/data/" + getPackageName() + "/databases/you_db_file.db");
    byte[] mBuffer = new byte[1024];
    while (true) {
        int mLength = mInput.read(mBuffer);
        if (mLength > 0) {
            mOutput.write(mBuffer, 0, mLength);
        } else {
            mOutput.flush();
            mOutput.close();
            mInput.close();
            return;
        }
    }
}

Solution 5:

this is the correct way of doing it, use getDatabasePath()

    String dbFilePath = getDatabasePath("main.db").getAbsolutePath();
    File dbFile = new File(dbFilePath);
    try {
        InputStream inputStream = getAssets().open("main.db");
        OutputStream outputStream = new FileOutputStream(dbFile);
        byte[] buffer = new byte[8192];
        int read;
        while ((read = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, read);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new IllegalStateException("failed to copy db file");
    }

Post a Comment for "Copy SQLite Database From Assets Folder"