コード例 #1
0
  @Override
  public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

    Log.d(TAG, "update(uri=" + uri + ", values=" + values.toString() + ")");

    // Open a read / write database to support the transaction.
    SQLiteDatabase db = myOpenHelper.getWritableDatabase();
    String rowID = "";
    int updateCount = 0;

    // If this is a row URI, limit the deletion to the specified row.
    switch (uriMatcher.match(uri)) {
      case CardCursor_SINGLE_ROW:
        rowID = uri.getPathSegments().get(1);
        selection =
            CardCursorContract.CardCursor.KeyColumns.KEY_ID
                + "="
                + rowID
                + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
        updateCount =
            db.update(
                CardCursorSQLiteOpenHelper.Tables.CardCursor, values, selection, selectionArgs);
        break;
      default:
        throw new IllegalArgumentException("Unsupported URI: " + uri);
    }

    if (updateCount > 0) {
      // Notify any observers of the change in the data set.
      getContext().getContentResolver().notifyChange(uri, null);
    }

    return updateCount;
  }
コード例 #2
0
  @Override
  public Uri insert(Uri uri, ContentValues values) {

    Log.d(TAG, "insert(uri=" + uri + ", values=" + values.toString() + ")");

    // Open a read / write database to support the transaction.
    SQLiteDatabase db = myOpenHelper.getWritableDatabase();

    // To add empty rows to your database by passing in an empty Content Values
    // object, you must use the null column hack parameter to specify the name of
    // the column that can be set to null.
    String nullColumnHack = null;
    long id = -1;

    if (uriMatcher.match(uri) == CardCursor_ALLROWS) {
      // Insert the values into the table
      id = db.insert(CardCursorSQLiteOpenHelper.Tables.CardCursor, nullColumnHack, values);
    }

    if (id > -1) {
      // Construct and return the URI of the newly inserted row.
      // Equals to Uri.parse(BASE_PATH + "/" + id);
      Uri insertedId = ContentUris.withAppendedId(uri, id);

      // Notify any observers of the change in the data set.
      getContext().getContentResolver().notifyChange(insertedId, null);

      return insertedId;
    } else throw new SQLException("Problem while inserting into uri: " + uri);
  }
コード例 #3
0
  @Override
  public int delete(Uri uri, String selection, String[] selectionArgs) {

    Log.d(TAG, "delete(uri=" + uri + ")");

    // Open a read / write database to support the transaction.
    SQLiteDatabase db = myOpenHelper.getWritableDatabase();
    String rowID = "";
    int deleteCount = 0;

    // If this is a row URI, limit the deletion to the specified row.
    switch (uriMatcher.match(uri)) {
      case CardCursor_ALLROWS:
        deleteCount =
            db.delete(CardCursorSQLiteOpenHelper.Tables.CardCursor, selection, selectionArgs);
        break;
      case CardCursor_SINGLE_ROW:
        rowID = uri.getPathSegments().get(1);
        selection =
            CardCursorContract.CardCursor.KeyColumns.KEY_ID
                + "="
                + rowID
                + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
        deleteCount =
            db.delete(CardCursorSQLiteOpenHelper.Tables.CardCursor, selection, selectionArgs);
        break;
      default:
        throw new IllegalArgumentException("Unsupported URI: " + uri);
    }

    // To return the number of deleted items, you must specify a where
    // clause. To delete all rows and return a value, pass in "1".
    // if (selection == null)
    //    selection = "1";

    if (deleteCount > 0) {
      // Notify any observers of the change in the data set.
      getContext().getContentResolver().notifyChange(uri, null);
    }

    return deleteCount;
  }
コード例 #4
0
  @Override
  public Cursor query(
      Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    Log.d(TAG, "query(uri=" + uri + ", proj=" + Arrays.toString(projection) + ")");
    // Open a read-only database.
    SQLiteDatabase db = myOpenHelper.getWritableDatabase();

    // Replace these with valid SQL statements if necessary.
    String groupBy = null;
    String having = null;

    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

    String rowID = "";
    // If this is a row query, limit the result set to the passed in row.
    switch (uriMatcher.match(uri)) {
      case CardCursor_SINGLE_ROW:
        queryBuilder.setTables(CardCursorSQLiteOpenHelper.Tables.CardCursor);
        rowID = uri.getPathSegments().get(1);
        queryBuilder.appendWhere(CardCursorContract.CardCursor.KeyColumns.KEY_ID + "=" + rowID);
        break;
      case CardCursor_ALLROWS:
        queryBuilder.setTables(CardCursorSQLiteOpenHelper.Tables.CardCursor);
        if (TextUtils.isEmpty(sortOrder)) {
          sortOrder = CardCursorContract.CardCursor.DEFAULT_SORT;
        }
        break;
      default:
        throw new IllegalArgumentException("Unsupported URI: " + uri);
    }

    Cursor cursor =
        queryBuilder.query(db, projection, selection, selectionArgs, groupBy, having, sortOrder);

    // Make sure that potential listeners are getting notified
    cursor.setNotificationUri(getContext().getContentResolver(), uri);

    return cursor;
  }