1

I followed this tutorial at this page. I followed it thoroughly but I still can't get the prepopulated database file to work. I keep getting an error saying .getReadableDatabase or .getWritableDatabase is called recursively. Also tried the solution of the answer for this page and also it doesn't work. Is there a simpler way to copy a prepopulated database to a local database which enables me to update and create data?

Community
  • 1
  • 1
mLjH
  • 11
  • 2

1 Answers1

0

Try this :

public class DatabaseManager {
private DatabaseHelper dataHelper;
private SQLiteDatabase mDb;
private Context ctx;
private String DATABASE_PATH = "/data/data/Your_Package_Name/databases/";
private static String DATABASE_NAME = "Your_Database";
private static String TABLE_NAME = "Your_Table";
private static final int DATABASE_VERSION = 1;
String Class_Tag = "DatabaseManager";

public DatabaseManager(Context ctx) {
    this.ctx = ctx;
    dataHelper = new DatabaseHelper(ctx);
}

private static class DatabaseHelper extends SQLiteOpenHelper {
    @SuppressWarnings("unused")
    Context myContext = null;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.myContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w("DBHelper", "Upgrading database from version " + oldVersion
                + " to " + newVersion + ", which will destroy all old data");
        onCreate(db);
    }
}

public boolean checkDataBase() {

    File f = null;
    try {
        String myPath = DATABASE_PATH + DATABASE_NAME;
        f = new File(myPath);
    } catch (Exception e) {
        Log.e(Class_Tag, "checkDataBase()", e);
    }
    return f.exists();
}

public void createDataBase() {

    try {
        openDB();
        InputStream myInput = ctx.getAssets().open(DATABASE_NAME + ".db");
        OutputStream myOutput = new FileOutputStream(DATABASE_PATH
                + DATABASE_NAME);

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

        if (mDb.isOpen())
            mDb.close();
        myOutput.flush();
        myOutput.close();
        myInput.close();

    } catch (Exception e) {
        Log.e(Class_Tag, "createDataBase()", e);
    }
}

public DatabaseManager openDB() throws SQLException {

    mDb = dataHelper.getWritableDatabase();
    return this;
}
public void closeDB() {
    try {
        if (mDb != null) {
            mDb.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

and in MainActivity

DatabaseManager dbMgr=new DatabaseManager(this);
try {
    if (!dbMgr.checkDataBase()) {
                dbMgr.createDataBase();
            }
        } catch (Exception e) {
            Log.e(Class_Tag, "onCreate()", e);
        } finally {
            dbMgr.closeDB();
        }

Hope it helps...

GAMA
  • 5,958
  • 14
  • 79
  • 126