1

I'm trying to use sqlite helper class, I have my db_map at /assets folder. sqlite helper should copy it from there to data/data/com.package.app/databases/

what happens is that I get a FileNotFound exception.

While debugging and using assetManager.list(""). I found that I have "images", "sounds", "webkit" files in the list. what is weird is I don't have those and only my db_map file.

code :

AssetManager assetManager = context.getResources().getAssets();
myInput = assetManager.open("db_map");
yosher lutski
  • 306
  • 2
  • 8

1 Answers1

0

First of all You need to create a database in the destination. After that you can copy of top of it.

So. this is what i use.

I created a class Database that extends from SQLiteOpenHelper

In this class i do the following.

// Database Version
private static final int DB_VERSION = 1;

// Database Name
private static String DB_NAME = "DB.sqlite";

//The default system path of your application database.
private static String DB_PATH = "/data/data/com.namespace.xxx/databases/";

public Database(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    this.myContext = context;
}   

public void createDataBase() throws IOException{

    Log.d(LOG, "Calling checkDataBase() Method");

    boolean dbExist = checkDataBase();

    if(dbExist){
        //do nothing - database already exist
        Log.d(LOG, "!!!Database Found!!!");
    }else{
        //Empty database will be created into the default system path
        Log.d(LOG, "!!!Creating Empy Database!!!");
        this.getReadableDatabase();
        this.close();
        try {
            Log.d(LOG, "!!!Coping Database!!!");
            copyDataBase();
        } catch (IOException e) {
            throw new Error(e);
        }
    } 
}

private boolean checkDataBase(){

    SQLiteDatabase checkDB = null;

    try{
        String myPath = DB_PATH + DB_NAME;

        Log.d(LOG, "looking database at " + myPath);

        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }catch(SQLiteException e){
        //database does't exist yet.
        Log.e(LOG, "Exception: database does not exist yet");
    }

    if(checkDB != null){
        checkDB.close();
    }
    return checkDB != null ? true : false;
}

// Copies your database from your local assets-folder
// to the created empty database
private void copyDataBase() throws IOException{
    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    Log.d(LOG, "Coping database from " + myInput + ", to " + outFileName);

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

Good Luck.

Gatunox
  • 532
  • 4
  • 14