1

I want to copy my sample database from my asset folder to the /data/data/packagename/databases/ directory to use it later in the application.

I have viewed many tutorial and other solution like here( Copy Database from assets folder in unrooted device) and here (copy database from assets to databases folder) but it not work for me. I have no error but the database that has been copied is empty. Is there something wrong with my coding?

Here is my coding for copying the database.

       @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    setContentView(R.layout.activity_dictionary_type);

    File dbfile=new File("data/data/com.example.myidictionary/databases","DefinitionDB");

    if (!dbfile.exists()) {
        try {
            copyDatabase(dbfile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Toast.makeText(getApplicationContext(), "Sample data is being copied", Toast.LENGTH_LONG).show();
    }
    else
        Toast.makeText(getApplicationContext(), "Database Exist", Toast.LENGTH_LONG).show();

}

private void copyDatabase(File dbFile) throws IOException 
{
    InputStream is = this.getAssets().open("DefinitionDB");
    OutputStream os = new FileOutputStream(dbFile);

    byte[] buffer = new byte[1024];

    while (is.read(buffer) > 0) 
    {
        os.write(buffer);
    }

    os.flush();
    os.close();
    is.close();
}
Community
  • 1
  • 1
Fitri Izuan
  • 350
  • 1
  • 6
  • 18
  • 1
    There's an open source library that you can use to copy the database from the assets folder: https://github.com/jgilfelt/android-sqlite-asset-helper – rubenlop88 Nov 26 '13 at 02:52
  • Is there any sample on how to use the open source library to copy the database from asset folder? I am still newbie in this field and still has a lot to learn. – Fitri Izuan Nov 26 '13 at 03:04
  • also make sure that u give permissions to mkdir - that is set the parent dir creations to true. – Rat-a-tat-a-tat Ratatouille Nov 26 '13 at 04:55
  • Examples here: https://github.com/jgilfelt/android-sqlite-asset-helper#usage and here: https://github.com/jgilfelt/android-sqlite-asset-helper/tree/master/example-v1 – rubenlop88 Nov 26 '13 at 14:59

1 Answers1

4

Try below code and let me know whether is it working properly?

 public class DataBaseWrapper extends SQLiteOpenHelper
 {
  private static String TAG = DataBaseWrapper.class.getName();
  private  String DB_PATH; //= "/data/data/com.example.yourproject/databases/";
  private static String DB_NAME = "Database.sqlite";
  private SQLiteDatabase myDataBase = null; 
  private final Context myContext;

  public DataBaseWrapper(Context context) 
  {
     super(context, DB_NAME, null, 1);

      this.myContext = context;
      DB_PATH="/data/data/" + context.getPackageName() + "/" + "databases/";
      Log.v("log_tag", "DBPath: " + DB_PATH);
     //  File f=getDatabasePath(DB_NAME);
  } 

  public void createDataBase() throws IOException{
   boolean dbExist = checkDataBase();
   if(dbExist){
    Log.v("log_tag", "database does exist");
    }else{
     Log.v("log_tag", "database does not exist");
     this.getReadableDatabase();
     try {
      copyDataBase();
        } catch (IOException e) {
      throw new Error("Error copying database");
      }
    }
   }

  private void copyDataBase() throws IOException{
  InputStream myInput = myContext.getAssets().open(DB_NAME);
  String outFileName = DB_PATH + DB_NAME;
  OutputStream myOutput = new FileOutputStream(outFileName);
  byte[] buffer = new byte[1024];
  int length;
   while ((length = myInput.read(buffer))>0){
    myOutput.write(buffer, 0, length);
   }
   myOutput.flush();
   myOutput.close();
   myInput.close();
  }

  private boolean checkDataBase(){

     File dbFile = new File(DB_PATH + DB_NAME); 
     //Log.v("dbFile", dbFile + "   "+ dbFile.exists()); 
     return dbFile.exists(); 

 }

 public boolean openDataBase() throws SQLException
 {
    String mPath = DB_PATH + DB_NAME; 
    //Log.v("mPath", mPath); 
    myDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY); 
    //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
    return myDataBase != null; 

 }


  @Override
  public synchronized void close() 
  {
     if(myDataBase != null)
      myDataBase.close();
     super.close();
  }

 @Override
 public void onCreate(SQLiteDatabase db) 
 {


  }

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
 {
    Log.v(TAG, "Upgrading database, this will drop database and recreate.");
  }
  }
Jigar Pandya
  • 2,129
  • 2
  • 17
  • 27