Android - Error code 11 when importing Sqlite database into Galaxy Note

I use the code below to import a pre-filled (ORMLite) database from assets.

This works great on multiple devices, with the exception of the Galaxy Note 10.1. Here I get an exception when the database closes after the operation is completed:

"error code = 11, msg = database corruption in line ...."

When I download the database from the device and open it in SqliteBrowser, everything seems fine. Any ideas?

public class MySQLiteOpenHelper extends SQLiteOpenHelper { // {....} public void importDB() { InputStream is = context.getAssets().open("DBName.db"); try { close(); FileOutputStream os = new FileOutputStream(new File(Environment.getDataDirectory(), dbPath)); try { byte[] buffer = new byte[4096]; for (int n; (n = is.read(buffer)) != -1;) { os.write(buffer, 0, n); } } finally { os.close(); } } finally { is.close(); } getWritableDatabase().close(); } 

Error:

 I/SqliteDatabaseCpp(684): sqlite returned: error code = 11, msg = database corruption at line 48171 of [ed759d5a9e], db=/data/data/app_name/databases/DBName_db I/SqliteDatabaseCpp(684): sqlite returned: error code = 11, msg = database disk image is malformed, db=/data/data/app_name/databases/DBName_db E/SqliteDatabaseCpp(684): sqlite3_exec - Failed to set synchronous mode = 1(Normal) I/SqliteDatabaseCpp(684): sqlite returned: error code = 11, msg = database corruption at line 48171 of [ed759d5a9e], db=/data/data/app_name/databases/DBName_db I/SqliteDatabaseCpp(684): sqlite returned: error code = 11, msg = database disk image is malformed, db=/data/data/app_name/databases/DBName_db E/SqliteDatabaseCpp(684): CREATE TABLE android_metadata failed E/DefaultDatabaseErrorHandler(684): Corruption reported by sqlite on database: /data/data/app.name/databases/DBName.db E/DefaultDatabaseErrorHandler(684): deleting the database file: /data/data/app.name/databases/DBName.db 
+4
source share
2 answers

You should always synchronize: os.getFD().sync() Sync os.getFD().sync() before closing FileOutputStream. This is due to the file system. Some of them write when they see fit - and this can cause problems.

0
source

As you can see from the output of logcat, you do not provide your own implementation for DatabaseErrorHandler and the implicit handler (DefaultDatabaseErrorHandler), you just delete the damaged database. You must use your implementation of DefaultDatabaseErrorHandler

You must run PRAGMA integrity_check; on the restored database. Usually this returns the string "ok" unless otherwise you can parse the string and, for example, if you find an index that you can drop and recreate.

 public class MySQLiteOpenHelper extends SQLiteOpenHelper { public MySQLiteOpenHelper(final Context context) { super(context, DB_NAME, null, DATABASE_VERSION, new DatabaseErrorHandler() { @Override public void onCorruption(SQLiteDatabase db) { //your code } }); } // {....} } 
0
source

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


All Articles