下面列出了android.database.sqlite.SQLiteCantOpenDatabaseException#android.database.sqlite.SQLiteDiskIOException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static final void readExceptionFromParcel(Parcel reply, String msg, int code) {
switch (code) {
case 2:
throw new IllegalArgumentException(msg);
case 3:
throw new UnsupportedOperationException(msg);
case 4:
throw new SQLiteAbortException(msg);
case 5:
throw new SQLiteConstraintException(msg);
case 6:
throw new SQLiteDatabaseCorruptException(msg);
case 7:
throw new SQLiteFullException(msg);
case 8:
throw new SQLiteDiskIOException(msg);
case 9:
throw new SQLiteException(msg);
case 11:
throw new OperationCanceledException(msg);
default:
reply.readException(code, msg);
}
}
private void writeToDb(Object o, Bitmap preview) {
String name = getObjectName(o);
SQLiteDatabase db = mDb.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(CacheDb.COLUMN_NAME, name);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
preview.compress(Bitmap.CompressFormat.PNG, 100, stream);
values.put(CacheDb.COLUMN_PREVIEW_BITMAP, stream.toByteArray());
values.put(CacheDb.COLUMN_SIZE, mSize);
try {
db.insert(CacheDb.TABLE_NAME, null, values);
} catch (SQLiteDiskIOException e) {
recreateDb();
}
}
public static void removePackageFromDb(final CacheDb cacheDb, final String packageName) {
synchronized(sInvalidPackages) {
sInvalidPackages.add(packageName);
}
new AsyncTask<Void, Void, Void>() {
public Void doInBackground(Void ... args) {
SQLiteDatabase db = cacheDb.getWritableDatabase();
try {
db.delete(CacheDb.TABLE_NAME,
CacheDb.COLUMN_NAME + " LIKE ? OR " +
CacheDb.COLUMN_NAME + " LIKE ?", // SELECT query
new String[] {
WIDGET_PREFIX + packageName + "/%",
SHORTCUT_PREFIX + packageName + "/%"
} // args to SELECT query
);
} catch (SQLiteDiskIOException e) {
}
synchronized(sInvalidPackages) {
sInvalidPackages.remove(packageName);
}
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
}
/**
* Special function for writing an exception result at the header of
* a parcel, to be used when returning an exception from a transaction.
* exception will be re-thrown by the function in another process
* @param reply Parcel to write to
* @param e The Exception to be written.
* @see Parcel#writeNoException
* @see Parcel#writeException
*/
public static final void writeExceptionToParcel(Parcel reply, Exception e) {
int code = 0;
boolean logException = true;
if (e instanceof FileNotFoundException) {
code = 1;
logException = false;
} else if (e instanceof IllegalArgumentException) {
code = 2;
} else if (e instanceof UnsupportedOperationException) {
code = 3;
} else if (e instanceof SQLiteAbortException) {
code = 4;
} else if (e instanceof SQLiteConstraintException) {
code = 5;
} else if (e instanceof SQLiteDatabaseCorruptException) {
code = 6;
} else if (e instanceof SQLiteFullException) {
code = 7;
} else if (e instanceof SQLiteDiskIOException) {
code = 8;
} else if (e instanceof SQLiteException) {
code = 9;
} else if (e instanceof OperationApplicationException) {
code = 10;
} else if (e instanceof OperationCanceledException) {
code = 11;
logException = false;
} else {
reply.writeException(e);
Log.e(TAG, "Writing exception to parcel", e);
return;
}
reply.writeInt(code);
reply.writeString(e.getMessage());
if (logException) {
Log.e(TAG, "Writing exception to parcel", e);
}
}
@Test
public void testDatabaseIsCorrupt() throws IOException {
mDatabase.execSQL("create table t (i int);");
// write junk into the database file
BufferedWriter writer = new BufferedWriter(new FileWriter(mDatabaseFile.getPath()));
writer.write("blah");
writer.close();
assertTrue(mDatabaseFile.exists());
// since the database file is now corrupt, doing any sql on this database connection
// should trigger call to MyDatabaseCorruptionHandler.onCorruption
try {
mDatabase.execSQL("select * from t;");
fail("expected exception");
} catch (SQLiteDiskIOException e) {
//
// this test used to produce a corrupted db. but with new sqlite it instead reports
// Disk I/O error. meh..
// need to figure out how to cause corruption in db
//
// expected
if (mDatabaseFile.exists()) {
mDatabaseFile.delete();
}
} catch (SQLiteException ignored) {
}
// database file should be gone
assertFalse(mDatabaseFile.exists());
// after corruption handler is called, the database file should be free of
// database corruption
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(mDatabaseFile.getPath(), null,
new MyDatabaseCorruptionHandler());
assertTrue(db.isDatabaseIntegrityOk());
}
private void clearDb() {
SQLiteDatabase db = mDb.getWritableDatabase();
// Delete everything
try {
db.delete(CacheDb.TABLE_NAME, null, null);
} catch (SQLiteDiskIOException e) {
}
}
public static void removeItemFromDb(final CacheDb cacheDb, final String objectName) {
new AsyncTask<Void, Void, Void>() {
public Void doInBackground(Void ... args) {
SQLiteDatabase db = cacheDb.getWritableDatabase();
try {
db.delete(CacheDb.TABLE_NAME,
CacheDb.COLUMN_NAME + " = ? ", // SELECT query
new String[] { objectName }); // args to SELECT query
} catch (SQLiteDiskIOException e) {
}
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
}