下面列出了怎么用android.database.sqlite.SQLiteFullException的API类实例代码及写法,或者点击链接到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 processLocations(List<Location> locations, Context context,
OpenLocate.Configuration configuration,
AdvertisingIdClient.Info advertisingIdInfo) {
LocationDatabase locationsDatabase = new LocationDatabase(DatabaseHelper.getInstance(context));
try {
for (Location location : locations) {
Log.v(TAG, location.toString());
OpenLocateLocation olLocation = OpenLocateLocation.from(
location,
advertisingIdInfo,
InformationFieldsFactory.collectInformationFields(context, configuration)
);
locationsDatabase.add(olLocation);
}
} catch (SQLiteFullException exception) {
Log.w(TAG, "Database is full. Cannot add data.");
} finally {
locationsDatabase.close();
}
}
private void handleError(Exception exception) {
Exception errProcessEx = exFiltrate(exception);
if (errProcessEx instanceof SQLiteFullException) {
// If the error is sqLite full exception already, no need to update it to the database
// again.
handleSQLiteFullException((SQLiteFullException) errProcessEx);
} else {
// Normal case.
try {
model.setStatus(FileDownloadStatus.error);
model.setErrMsg(exception.toString());
database.updateError(model.getId(), errProcessEx, model.getSoFar());
} catch (SQLiteFullException fullException) {
errProcessEx = fullException;
handleSQLiteFullException((SQLiteFullException) errProcessEx);
}
}
processParams.setException(errProcessEx);
onStatusChanged(FileDownloadStatus.error);
}
@Override
public boolean delete()
throws SQLiteException
{
try {
//drop table
MapContentProviderHelper map = (MapContentProviderHelper) MapBase.getInstance();
SQLiteDatabase db = map.getDatabase(false);
String tableDrop = "DROP TABLE IF EXISTS " + mPath.getName();
db.execSQL(tableDrop);
} catch (SQLiteFullException e) {
e.printStackTrace();
}
return super.delete();
}
/**
* 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);
}
}
private boolean performSetItem(String key, String value, boolean isPersistent, boolean allowRetryWhenFull) {
SQLiteDatabase database = mDatabaseSupplier.getDatabase();
if (database == null) {
return false;
}
WXLogUtils.d(WXSQLiteOpenHelper.TAG_STORAGE, "set k-v to storage(key:" + key + ",value:" + value + ",isPersistent:" + isPersistent + ",allowRetry:" + allowRetryWhenFull + ")");
String sql = "INSERT OR REPLACE INTO " + WXSQLiteOpenHelper.TABLE_STORAGE + " VALUES (?,?,?,?);";
SQLiteStatement statement = null;
String timeStamp = WXSQLiteOpenHelper.sDateFormatter.format(new Date());
try {
statement = database.compileStatement(sql);
statement.clearBindings();
statement.bindString(1, key);
statement.bindString(2, value);
statement.bindString(3, timeStamp);
statement.bindLong(4, isPersistent ? 1 : 0);
statement.execute();
return true;
} catch (Exception e) {
WXLogUtils.e(WXSQLiteOpenHelper.TAG_STORAGE, "DefaultWXStorage occurred an exception when execute setItem :" + e.getMessage());
if (e instanceof SQLiteFullException) {
if (allowRetryWhenFull && trimToSize()) {
//try again
//setItem/setItemPersistent method only allow try once when occurred a sqliteFullException.
WXLogUtils.d(WXSQLiteOpenHelper.TAG_STORAGE, "retry set k-v to storage(key:" + key + ",value:" + value + ")");
return performSetItem(key, value, isPersistent, false);
}
}
return false;
} finally {
if(statement != null) {
statement.close();
}
}
}
private boolean performSetItem(String key, String value, boolean isPersistent, boolean allowRetryWhenFull) {
SQLiteDatabase database = mDatabaseSupplier.getDatabase();
if (database == null) {
return false;
}
WXLogUtils.d(WXSQLiteOpenHelper.TAG_STORAGE, "set k-v to storage(key:" + key + ",value:" + value + ",isPersistent:" + isPersistent + ",allowRetry:" + allowRetryWhenFull + ")");
String sql = "INSERT OR REPLACE INTO " + WXSQLiteOpenHelper.TABLE_STORAGE + " VALUES (?,?,?,?);";
SQLiteStatement statement = null;
String timeStamp = WXSQLiteOpenHelper.sDateFormatter.format(new Date());
try {
statement = database.compileStatement(sql);
statement.clearBindings();
statement.bindString(1, key);
statement.bindString(2, value);
statement.bindString(3, timeStamp);
statement.bindLong(4, isPersistent ? 1 : 0);
statement.execute();
return true;
} catch (Exception e) {
WXLogUtils.e(WXSQLiteOpenHelper.TAG_STORAGE, "DefaultWXStorage occurred an exception when execute setItem :" + e.getMessage());
if (e instanceof SQLiteFullException) {
if (allowRetryWhenFull && trimToSize()) {
//try again
//setItem/setItemPersistent method only allow try once when occurred a sqliteFullException.
WXLogUtils.d(WXSQLiteOpenHelper.TAG_STORAGE, "retry set k-v to storage(key:" + key + ",value:" + value + ")");
return performSetItem(key, value, isPersistent, false);
}
}
return false;
} finally {
if(statement != null) {
statement.close();
}
}
}
/**
* 数据库文件已达到最大空间(数据库已满)
*
* @param e
*/
public static void printStackTrace(String TAG, SQLiteFullException e) {
if (IsDebug) {
e.printStackTrace();
} else {
logException(TAG, e);
}
}
public static void logException(Context ctx, Exception ex, String message, Object... extras) {
message = forException(ex, message, extras);
i(LOG_TAG, message, ex);
// Do not try to save SQLiteFullException to the database - this
// will (unsurprisingly) fail if the database is full
if(!(ex instanceof SQLiteFullException)) {
eventLogEntry(ctx, message);
}
}
public static void setPicture(MediaWrapper m, Bitmap p) {
Log.d(TAG, "Setting new picture for " + m.getTitle());
try {
getInstance().updateMedia(
m.getUri(),
INDEX_MEDIA_PICTURE,
p);
} catch (SQLiteFullException e) {
Log.d(TAG, "SQLiteFullException while setting picture");
}
m.setPictureParsed(true);
}
private void handleSQLiteFullException(final SQLiteFullException sqLiteFullException) {
final int id = model.getId();
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "the data of the task[%d] is dirty, because the SQLite "
+ "full exception[%s], so remove it from the database directly.",
id, sqLiteFullException.toString());
}
model.setErrMsg(sqLiteFullException.toString());
model.setStatus(FileDownloadStatus.error);
database.remove(id);
database.removeConnections(id);
}
public static void delete(String tableName)
{
try {
MapContentProviderHelper map = (MapContentProviderHelper) MapBase.getInstance();
SQLiteDatabase db = map.getDatabase(true);
String tableDrop = "DROP TABLE IF EXISTS " + tableName;
db.execSQL(tableDrop);
} catch (SQLiteFullException | SQLiteReadOnlyDatabaseException e) {
e.printStackTrace();
}
}
private void onDiskFull(SQLiteFullException e) {
mIgnoreWrites = true;
}
public static boolean sendLocations(Context context, List<OpenLocate.Endpoint> endpoints) {
boolean isSuccess = true;
SQLiteOpenHelper helper = DatabaseHelper.getInstance(context);
LocationDataSource dataSource = new LocationDatabase(helper);
HttpClient httpClient = new HttpClientImpl();
LocationDispatcher dispatcher = new LocationDispatcher();
String userAgent = getUserAgent(context);
List<Long> timestamps = new ArrayList<>(endpoints.size());
for (OpenLocate.Endpoint endpoint : endpoints) {
String key = md5(endpoint.getUrl().toLowerCase());
try {
long timestamp = SharedPreferenceUtils.getInstance(context).getLongValue(key, 0);
List<OpenLocateLocation> sentLocations = dispatcher.postLocations(httpClient, endpoint, userAgent, timestamp, dataSource);
if (sentLocations != null && sentLocations.isEmpty() == false) {
long latestCreatedLocationDate =
sentLocations.get(sentLocations.size() - 1).getCreated().getTime();
SharedPreferenceUtils.getInstance(context).setValue(key, latestCreatedLocationDate);
} else if (sentLocations != null && sentLocations.isEmpty()) {
isSuccess = false;
}
} catch (Exception e) {
e.printStackTrace();
}
timestamps.add(SharedPreferenceUtils.getInstance(context).getLongValue(key, 0));
}
Long min = Collections.min(timestamps);
if (min != null) {
long expired = System.currentTimeMillis() - EXPIRED_PERIOD;
if (min < expired) {
min = expired;
}
try {
dataSource.deleteBefore(min);
} catch (SQLiteFullException exception) {
Log.w(TAG, "Database is full. Cannot purge data.");
} finally {
dataSource.close();
}
}
return isSuccess;
}