Skip to content

Instantly share code, notes, and snippets.

@Axxiss
Created May 4, 2012 10:20
Show Gist options
  • Save Axxiss/2593841 to your computer and use it in GitHub Desktop.
Save Axxiss/2593841 to your computer and use it in GitHub Desktop.
CRUD
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count;
switch (uriMatcher.match(uri)) {
case LOG_LIST:
count = dbLogs.delete(TABLE_NAME, selection, selectionArgs);
break;
case LOG_ITEM:
String segment = uri.getPathSegments().get(1);
count = dbLogs.delete(TABLE_NAME,
KEY_ID + " = " + segment
+ (!TextUtils.isEmpty(selection) ? " AND (" + selection + ")" : "")
, selectionArgs);
break;
default: throw new IllegalArgumentException("Unsupported URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public Uri insert(Uri uri, ContentValues values){
long rowID = dbLogs.insert(TABLE_NAME, null, values);
if (rowID > 0)
{
Uri result = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(result, null);
return result;
}
throw new SQLException("Failed to insert row into " + uri);
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int count;
switch (uriMatcher.match(uri)) {
case LOG_LIST:
count = dbLogs.update(TABLE_NAME, values, selection, selectionArgs);
break;
case LOG_ITEM:
String segment = uri.getPathSegments().get(1);
count = dbLogs.update(TABLE_NAME,
values,
KEY_ID + " = " + segment
+ (!TextUtils.isEmpty(selection) ? " AND (" + selection + ")" : "")
, selectionArgs);
break;
default: throw new IllegalArgumentException("Unsupported URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public Cursor query(Uri uri,
String[] projection,
String selection,
String[] selectionArgs,
String sortOrder) {
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(TABLE_NAME);
switch (uriMatcher.match(uri)) {
case LOG_ITEM:
builder.appendWhereEscapeString(KEY_ID + " = " + uri.getPathSegments().get(1));
break;
}
Cursor cursor = builder.query(
dbLogs,
projection,
selection,
selectionArgs,
null, null, sortOrder);
// Register the contexts ContentResolver to be notified if
// the cursor result set changes.
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment