6

I want to use preloaded database in my app means trying to get database at the time the apk is installed so can use the data already saved in that. I copy the "ingredients.db" file in the assets folder. And Use the following code but this get the error " Problem Copying Database From Resource File "

How can i solve this problem? please suggest me the ways possible

My Database Helper class is like that

class IngredientHelper extends SQLiteOpenHelper {
        private static final String DATABASE_PATH = "/data/data/com.example.preloadeddatabase/databases/";
        private static final String DATABASE_NAME = "ingredients.db";

        private static final String TABLE_NAME = "Ingredients";
        private static final String COLUMN_ID = "_id";
        private static final String COLUMN_TITLE = "ingredient_name";

        private static final int SCHEMA_VERSION = 1;

        public SQLiteDatabase dbSqlite;
        private final Context myContext;

        public IngredientHelper(Context context) {
            super(context, DATABASE_NAME, null, SCHEMA_VERSION);
            this.myContext = context;

        }

        @Override
        public void onCreate(SQLiteDatabase db) {

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }

        public void createDatabase() {

            createDB();
        }

        public void createDB() {

            boolean dbExist = DbExists();

            if (!dbExist) {
                this.getReadableDatabase();
                copyDataBase();

            }
        }

        private boolean DbExists() {
            SQLiteDatabase db = null;
            try {
                String databasePath = DATABASE_PATH + DATABASE_NAME;
                db = SQLiteDatabase.openDatabase(databasePath, null,
                        SQLiteDatabase.OPEN_READWRITE);

                db.setLocale(Locale.getDefault());
                db.setLockingEnabled(true);
                db.setVersion(1);

            }

            catch (SQLiteException e) {
                Log.e("SqlHelper", "Database Not Found");
            }

            if (db != null) {
                db.close();
            }

            return db != null ? true : false;
        }

        private void copyDataBase() {
            InputStream iStream = null;
            OutputStream oStream = null;
            String outFilePath = DATABASE_PATH + DATABASE_NAME;
            try {
                iStream = myContext.getAssets().open(DATABASE_NAME);
                oStream = new FileOutputStream(outFilePath);
                byte[] buffer = new byte[2048];
                int length;
                while ((length = iStream.read(buffer)) > 0) {
                    oStream.write(buffer, 0, length);
                }
                oStream.flush();
                oStream.close();
                iStream.close();
            }

            catch (IOException e) {
                throw new Error("Problem Copying Database From Resource File");
            }
        }

        public void openDatabase() throws SQLException {

            String myPath = DATABASE_PATH + DATABASE_NAME;
            dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READWRITE);
        }

        @Override
        public synchronized void close() {

            if (dbSqlite != null) {
                dbSqlite.close();
            }
            super.close();

        }

        public Cursor getCursor() {
            SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
            queryBuilder.setTables(TABLE_NAME);
            String[] asColumnsToReturn = new String[] { COLUMN_ID, COLUMN_TITLE };

            Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn,
                    null, null, null, null, "ingredient_name ASC");

            return mCursor;
        }

        public String getName(Cursor c) {
            return (c.getString(1));
        }
skaffman
  • 398,947
  • 96
  • 818
  • 769
MKJParekh
  • 34,073
  • 11
  • 87
  • 98

3 Answers3

2

Don't open the readable database until after you copy the resource file to your database directory. It is likely that you're getting the exception since you're trying to write to a file you already have open (since it gets opened when you call getReadableDatabase()).

Chris
  • 22,923
  • 4
  • 56
  • 50
1

Here is a simple solution to your problem, but in this method there is a mistake. When you use a method this.getReadableDatabase ();, then the database is open, ie it should be close this.close (); This error is especially important for devices HTC Desire

Use this solution, there solve the problem opening DB.

Roman
  • 1,045
  • 1
  • 12
  • 33
0

the database file must have extension ".sqlite" instead ".db"

jazz
  • 1,216
  • 7
  • 7