Copying sqlite db to Android device fails

I have sqlite db, which I copied to the asset folder. In my program, I check if the database exists if I do not create a new one and copy it. I used (more or less) the code from http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-2/

public void createDataBase() throws IOException { boolean dbExist = checkDataBase(); if (dbExist) { Log.i(DEBUG_TAG, "createDataBase -> Datenbank existiert"); // do nothing - database already exist } else { // By calling this method and empty database will be created into // the default system path // of your application so we are gonna be able to overwrite that // database with our database. this.getReadableDatabase(); Log.i(DEBUG_TAG, "else, db existiert nicht 1"); try { copyDataBase(); Log.i(DEBUG_TAG, "nach copydatabase"); } catch (IOException e) { throw new Error("Error copying database"); } } } /** * Checked ob Database bereits existiert * * @return */ private boolean checkDataBase() throws SQLiteException { SQLiteDatabase checkDB = null; Boolean checkTable = false; try { String myPath = DB_PATH + DB_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); Log.i(DEBUG_TAG, "checkdatabase1"); } catch (SQLiteException e) { Log.e(DEBUG_TAG, "Fehler checkDataBase: ", e); } if (checkDB != null) { checkDB.close(); } return checkDB != null ? true : false; } /** * Copies your database from your local assets-folder to the just created * empty database in the system folder, from where it can be accessed and * handled. This is done by transfering bytestream. * */ private void copyDataBase() throws IOException { // Open your local db as the input stream InputStream myInput = dbContext.getAssets().open(DB_NAME); // Path to the just created empty db String outFileName = DB_PATH + DB_NAME; // 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(); Log.i(DEBUG_TAG, "copydatabase"); } 

In the emulator, it works fine.

I tried to run it on my device (HTC Desire HD). There I get the following message:

 03-26 17:02:05.053: INFO/Database(24458): sqlite returned: error code = 14, msg = cannot open file at line 27206 of [42537b6056] 

This happens when I first try to open a database. When I run the Program in the emulator, I do not receive this message. When I run the program a second time, it finds a database, no errors with opening, but tables do not exist.

I debugged the program several times on the emulator and device, but did not find any solution.

Could this be a resolution problem? (Since I also cannot see db on a device with adb -> denied 'permission)

I am very new to android, so maybe I just missed something stupid.

thanks

+4
source share
2 answers
  • Do not create paths using string concatenation. Use the appropriate File constructor.
  • Your DB_PATH may not be valid, but since you refused to show it, we cannot say for sure.
  • If the class from which this code comes is equal to SQLiteOpenHelper , your call to getReadableDatabase() will create an empty database.
  • "(since I also can’t see db on the device with adb → denied 'permission) - this is expected since you do not have permission to view this directory on the production device.
0
source

I make it work by creating my own directory and putting the file in a directory

 String path=mContext.getDir("Folder_name", Context.MODE_WORLD_WRITEABLE).getAbsolutePath(); 

Code to create a folder.

Then complete our database validation and database copy operation.

 enter code here public void createDataBase() throws IOException { String path=mContext.getDir("Folder_Name", Context.MODE_WORLD_WRITEABLE).getAbsolutePath(); DB_PATH=path; boolean mDataBaseExist = checkDataBase(); if(!mDataBaseExist) { try { copyDataBase(); } catch (IOException mIOException) { Log.d("Exception",mIOException.getMessage()); throw new Error("ErrorCopyingDataBase"); } } } private boolean checkDataBase() { Log.d(TAG, "In checkDataBase :::::"); File dbFile = new File( DB_PATH+DATABASE_NAME); Log.d("dbFile", dbFile + " "+ dbFile.exists()); return dbFile.exists(); } //Copy the database from assets private void copyDataBase() throws IOException { Log.d(TAG, "In copyDataBase :::::"); InputStream mInput = mContext.getAssets().open(DB_NAME); String outFileName = DB_PATH + DATABASE_NAME; Log.d(TAG, "In copyDataBase outFileName:::::"+outFileName); OutputStream mOutput = new FileOutputStream(outFileName); byte[] mBuffer = new byte[1024]; int mLength; while ((mLength = mInput.read(mBuffer))>0) { mOutput.write(mBuffer, 0, mLength); } mOutput.flush(); mOutput.close(); mInput.close(); } //Open the database, so we can query it public boolean openDataBase() throws SQLException { //DB_PATH + String mPath = DB_PATH+ DATABASE_NAME; Log.v("mPath", mPath); mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY); return mDataBase != null; } 

Hope this helps new visitors.

Greetings

0
source

Source: https://habr.com/ru/post/1345359/


All Articles