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.
publicDataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
mContext = context;
}
@OverridepublicvoidonCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@OverridepublicvoidonUpgrade(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
**/publicvoidcreateDataBase()throws IOException {
booleanmDataBaseExist= checkDataBase();
if (!mDataBaseExist) {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException mIOException) {
mIOException.printStackTrace();
thrownewError("Error copying database");
} finally {
this.close();
}
}
}
/** This method checks whether database is exists or not **/privatebooleancheckDataBase() {
try {
finalStringmPath= DATABASE_PATH + DATABASE_NAME;
finalFilefile=newFile(mPath);
if (file.exists())
returntrue;
elsereturnfalse;
} catch (SQLiteException e) {
e.printStackTrace();
returnfalse;
}
}
/**
* This method will copy database from /assets directory to application
* package /databases directory
**/privatevoidcopyDataBase()throws IOException {
try {
InputStreammInputStream= mContext.getAssets().open(DATABASE_NAME);
StringoutFileName= DATABASE_PATH + DATABASE_NAME;
OutputStreammOutputStream=newFileOutputStream(outFileName);
byte[] buffer = newbyte[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 **/publicbooleanopenDataBase()throws SQLException {
StringmPath= DATABASE_PATH + DATABASE_NAME;
myDataBase = SQLiteDatabase.openDatabase(mPath, null,
SQLiteDatabase.OPEN_READWRITE);
return myDataBase.isOpen();
}
/** This method close database connection and released occupied memory **/@Overridepublicsynchronizedvoidclose() {
if (myDataBase != null)
myDataBase.close();
SQLiteDatabase.releaseMemory();
super.close();
}
Solution 2:
Here is my working code..
voidcopyDataBase()throws IOException {
StringoutFileName= DB_PATH + DB_NAME;
OutputStreammyOutput=newFileOutputStream(outFileName);
byte[] buffer = newbyte[1024];
int length;
InputStreammyInput= myContext.getAssets().open("mydb.sqlite");
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
}
Solution 3:
DataBaseHelpermyDbHelper=newDataBaseHelper();
myDbHelper = newDataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
thrownewError("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
For Copying the database these method is useful
privatevoidcopyDataBase()throws IOException
{
StringoutFileName= DATABASE_PATH + DATABASE_NAME;
OutputStreammyOutput=newFileOutputStream(outFileName);
InputStreammyInput= myContext.getAssets().open(DATABASE_NAME);
byte[] buffer = newbyte[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:
publicvoidCopyDatabase()throws IOException {
openOrCreateDatabase("you_db_file.db", 0, null);
InputStreammInput= getAssets().open("databases/forall.db");
OutputStreammOutput=newFileOutputStream("/data/data/" + getPackageName() + "/databases/you_db_file.db");
byte[] mBuffer = newbyte[1024];
while (true) {
intmLength= 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()
StringdbFilePath= getDatabasePath("main.db").getAbsolutePath();
FiledbFile=newFile(dbFilePath);
try {
InputStreaminputStream= getAssets().open("main.db");
OutputStreamoutputStream=newFileOutputStream(dbFile);
byte[] buffer = newbyte[8192];
int read;
while ((read = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, read);
}
} catch (Exception e) {
e.printStackTrace();
thrownewIllegalStateException("failed to copy db file");
}
Post a Comment for "Copy Sqlite Database From Assets Folder"