0

I am making an app which requires large set of data to be initialized when the app starts so I thought of exporting the initial db with the apk file in assets folder. I can copy that database to the working directory of mine, however the issue comes when I try to write/update anything to that database.

I do not know why it is saying that it's not writable. I know db in asset can not be rewritten. So now I thought of attaching the database with my internally created db (written through code) so that I could copy the tables with data with one go. I am not getting any code help for that.

halfer
  • 19,824
  • 17
  • 99
  • 186
Saty
  • 2,563
  • 3
  • 37
  • 88

2 Answers2

1

can follow this code it may be help you:

public class PrepopulatedDBOpenHelper extends SQLiteOpenHelper {
    public static final String DB_NAME = "task_management";
    public static String DB_PATH;
    private SQLiteDatabase database;
    private Context context;

    public static final String EMPLOYEE_TABLE = "employee";
    public static final String ID_FIELD = "_id";
    public static final String NAME_FIELD = "name";
    public static final String EMAIL_FIELD = "email";
    public static final String PHONE_FIELD = "phone";
    public static final String DESIGNATION_FIELD = "designation";

    public PrepopulatedDBOpenHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.context = context;

        // database path /data/data/pkg-name/databases/
        String packageName = context.getPackageName();
        DB_PATH = "/data/data/" + packageName + "/databases/";
        this.database = openDatabase();

    }

    public SQLiteDatabase getDatabase() {
        return this.database;
    }

    public SQLiteDatabase openDatabase() {
        String path = DB_PATH + DB_NAME;
        if (database == null) {
            createDatabase();
            database = SQLiteDatabase.openDatabase(path, null,
                    SQLiteDatabase.OPEN_READWRITE);
        }
        return database;
    }

    private void createDatabase() {
        boolean dbExists = checkDB();
        if (!dbExists) {
            this.getReadableDatabase();
            Log.e(getClass().getName(),
                    "Database doesn't exist. Copying database from assets...");
            copyDatabase();
        } else {
            Log.e(getClass().getName(), "Database already exists");
        }
    }

    private void copyDatabase() {
        try {
            InputStream dbInputStream = context.getAssets().open(DB_NAME);
            String path = DB_PATH + DB_NAME;
            OutputStream dbOutputStream = new FileOutputStream(path);
            byte[] buffer = new byte[4096];
            int readCount = 0;
            while ((readCount = dbInputStream.read(buffer)) > 0) {
                dbOutputStream.write(buffer, 0, readCount);
            }

            dbInputStream.close();
            dbOutputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private boolean checkDB() {
        String path = DB_PATH + DB_NAME;
        File file = new File(path);
        if (file.exists()) {
            Log.e(getClass().getName(), "Database already exists");
            return true;
        }
        Log.e(getClass().getName(), "Database does not exists");
        return false;
    }

    public synchronized void close() {
        if (this.database != null) {
            this.database.close();
        }
    }
   }
user3467178
  • 73
  • 3
  • 10
0

Can you try use this:

database = SQLiteDatabase.openDatabase(path, null,
                SQLiteDatabase.OPEN_READWRITE);
user3467178
  • 73
  • 3
  • 10
  • After copying to working directory? if yes, then tried with no result – Saty Mar 30 '14 at 04:25
  • I just tried to code posted in http://stackoverflow.com/questions/513084/how-to-ship-an-android-application-with-a-database and it seems to work thanx for helping.... – Saty Mar 30 '14 at 04:55