Ejemplo n.º 1
0
  // 插入数据
  @Override
  public Uri insert(Uri uri, ContentValues values) {
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    Uri insertUri = null;
    switch (MATCHER.match(uri)) {
      case PERSONS:
        long rowid = db.insert("person", "name", values);
        insertUri = ContentUris.withAppendedId(uri, rowid);

        break;

      default:
        throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());
    }
    return insertUri;
  }
Ejemplo n.º 2
0
  // 删除数据
  @Override
  public int delete(Uri uri, String selection, String[] selectionArgs) {
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    int count = 0;
    switch (MATCHER.match(uri)) {
      case PERSONS:
        count = db.delete("person", selection, selectionArgs);
        return count;

      case PERSON:
        long id = ContentUris.parseId(uri);
        String where = "_id=" + id;
        if (selection != null && !"".equals(selection)) {
          where = selection + " and " + where;
        }
        count = db.delete("person", where, selectionArgs);
        return count;

      default:
        throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());
    }
  }
Ejemplo n.º 3
0
  // 查询数据
  @Override
  public Cursor query(
      Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    SQLiteDatabase db = dbHelper.getWritableDatabase();
    switch (MATCHER.match(uri)) {
      case PERSONS:
        // 查询所有的数据
        return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);

      case PERSON:
        // 查询某个ID的数据
        // 通过ContentUris这个工具类解释出ID
        long id = ContentUris.parseId(uri);
        String where = " _id=" + id;
        if (!"".equals(selection) && selection != null) {
          where = selection + " and " + where;
        }

        return db.query("person", projection, where, selectionArgs, null, null, sortOrder);
      default:
        throw new IllegalArgumentException("unknow uri" + uri.toString());
    }
  }