@Override public int delete(Uri uri, String selection, String[] selectionArgs) { Log.d(TAG, "delete() uri " + uri); int uriType = sURIMatcher.match(uri); int rowsDeleted = 0; String id; SQLiteDatabase sqlDB = database.getWritableDatabase(); switch (uriType) { case EVENTS: rowsDeleted = sqlDB.delete(TABLE_EVENTS, selection, selectionArgs); break; case RECORDS: rowsDeleted = sqlDB.delete(TABLE_RECORDS, selection, selectionArgs); break; case EMPLOYEES: rowsDeleted = sqlDB.delete(TABLE_EMPLOYEES, selection, selectionArgs); break; case EVENT_ID: id = uri.getLastPathSegment(); rowsDeleted = sqlDB.delete(TABLE_EVENTS, EventManager.EventQuery.SELECTION_ID, new String[] {id}); break; case EMPLOYEE_ID: id = uri.getLastPathSegment(); rowsDeleted = sqlDB.delete(TABLE_EMPLOYEES, EventManager.EventQuery.SELECTION_ID, new String[] {id}); break; default: break; } getContext().getContentResolver().notifyChange(uri, null); Log.d(TAG, "delete() rowsDeleted " + rowsDeleted); return rowsDeleted; }
@Override public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); switch (uriMatcher.match(uri)) { case TEMP_MATTERS: qb.setTables(TempMatterTable.TABLE_NAME); qb.setProjectionMap(tempMatterProjectionMap); break; case TEMP_MATTER_ID: qb.setTables(TempMatterTable.TABLE_NAME); qb.setProjectionMap(tempMatterProjectionMap); qb.appendWhere(TempMatterTable._ID + "=" + uri.getPathSegments().get(1)); break; default: throw new IllegalArgumentException("Unknown URI " + uri); } String orderBy; if (TextUtils.isEmpty(sortOrder)) { orderBy = TempMatterTable.DEFAULT_SORT_ORDER; } else { orderBy = sortOrder; } SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy); // 为Cursor对象注册一个观察数据变化的URI c.setNotificationUri(getContext().getContentResolver(), uri); return c; }
@Override public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // TODO Auto-generated method stub SQLiteDatabase database = dbHelper.getWritableDatabase(); Cursor cursor = null; switch (uriMatcher.match(uri)) { case XidianData: cursor = database.query( "XidianData", projection, selection, selectionArgs, null, null, sortOrder); return cursor; case XidianDataItem: String dataId = uri.getPathSegments().get(1); cursor = database.query( "XidianData", projection, "id=?", new String[] {dataId}, null, null, sortOrder); return cursor; case XidianStudentData: cursor = database.query( "XidianStudentData", projection, selection, selectionArgs, null, null, sortOrder); return cursor; default: return null; } }
@Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { Log.d(TAG, "applyBatch()"); ContentProviderResult[] result = new ContentProviderResult[operations.size()]; int i = 0; // Opens the database object in "write" mode. SQLiteDatabase db = database.getWritableDatabase(); // Begin a transaction db.beginTransaction(); try { for (ContentProviderOperation operation : operations) { // Chain the result for back references result[i++] = operation.apply(this, result, i); } db.setTransactionSuccessful(); } catch (OperationApplicationException e) { Log.d(TAG, "batch failed: " + e.getLocalizedMessage()); } finally { db.endTransaction(); } Log.d(TAG, "applyBatch() result[0] " + result[0]); return result; }
@Override public Uri insert(Uri uri, ContentValues values) { Log.d(TAG, "insert()" + "uri = [" + uri + "], values = [" + values + "]"); int uriType = sURIMatcher.match(uri); long id = 0; // ziska odkaz na databazi SQLiteDatabase sqlDB = database.getWritableDatabase(); switch (uriType) { case EVENTS: // muze vlozit jen 1 zaznam id = sqlDB.insert(TABLE_EVENTS, null, values); break; case EMPLOYEES: id = sqlDB.insert(TABLE_EMPLOYEES, null, values); break; case RECORDS: id = sqlDB.insert(TABLE_RECORDS, null, values); default: break; } uri = Uri.withAppendedPath(uri, String.valueOf(id)); getContext().getContentResolver().notifyChange(uri, null); Log.d(TAG, "insert() uri " + uri); return uri; }
@Override public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = null; switch (uriMatcher.match(uri)) { case BOOK_DIR: cursor = db.query("Book", projection, selection, selectionArgs, null, null, sortOrder); break; case BOOK_ITEM: String bookId = uri.getPathSegments().get(1); cursor = db.query("Book", projection, "id = ?", new String[] {bookId}, null, null, sortOrder); break; case CATEGORY_DIR: cursor = db.query("Category", projection, selection, selectionArgs, null, null, sortOrder); break; case CATEGORY_ITEM: String categoryId = uri.getPathSegments().get(1); cursor = db.query( "Category", projection, "id = ?", new String[] {categoryId}, null, null, sortOrder); break; default: break; } return cursor; }
@Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = dbHelper.getWritableDatabase(); int count; switch (uriMatcher.match(uri)) { case TEMP_MATTERS: count = db.delete(TempMatterTable.TABLE_NAME, selection, selectionArgs); break; case TEMP_MATTER_ID: String profileId = uri.getPathSegments().get(1); count = db.delete( TempMatterTable.TABLE_NAME, TempMatterTable._ID + "=" + profileId + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs); break; default: throw new IllegalArgumentException("Unknow URI " + uri); } return count; }
public SampleDBManager open() { // open the database try { db = DBHelper.getWritableDatabase(); } catch (SQLException e) { e.printStackTrace(); } return this; }
@Override public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Log.d( TAG, "uri = [" + uri + "], projection = [" + projection + "], " + "selection = [" + selection + "], selectionArgs = [" + Arrays.toString(selectionArgs) + "], " + "sortOrder = [" + sortOrder + "]"); Log.d(TAG, "query() uri " + uri); SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); int uriType = sURIMatcher.match(uri); switch (uriType) { case EVENTS: // vsechny radky tabulky queryBuilder.setTables(TABLE_EVENTS); break; case RECORDS: // vsechny radky tabulky queryBuilder.setTables(TABLE_RECORDS); break; case EMPLOYEES: queryBuilder.setTables(TABLE_EMPLOYEES); break; default: break; } SQLiteDatabase db = database.getWritableDatabase(); Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder); Log.d(TAG, "query() cursor size " + cursor.getCount()); cursor.setNotificationUri(getContext().getContentResolver(), uri); return cursor; }
@Override public Uri insert(Uri uri, ContentValues values) { if (uriMatcher.match(uri) != TEMP_MATTERS) { throw new IllegalArgumentException("Unknown URI " + uri); } SQLiteDatabase db = dbHelper.getWritableDatabase(); long rowId = db.insert(TempMatterTable.TABLE_NAME, TempMatterTable.DESCRIPTION, values); if (rowId > 0) { Uri profileUri = ContentUris.withAppendedId(TempMatterTable.CONTENT_URI, rowId); getContext().getContentResolver().notifyChange(profileUri, null); return profileUri; } throw new SQLException("Failed to insert row into" + uri); }
@Override public int delete(Uri uri, String selection, String[] selectionArgs) { // TODO Auto-generated method stub SQLiteDatabase database = dbHelper.getWritableDatabase(); int delateRows = 0; switch (uriMatcher.match(uri)) { case XidianData: delateRows = database.delete("XidianData", selection, selectionArgs); return delateRows; case XidianDataItem: String dataId = uri.getPathSegments().get(1); delateRows = database.delete("XidianData", "id=?", new String[] {dataId}); return delateRows; case XidianStudentData: delateRows = database.delete("XidianStudentData", selection, selectionArgs); return delateRows; default: return 0; } }
@Override public Uri insert(Uri uri, ContentValues values) { Uri uriReturn = null; SQLiteDatabase db = dbHelper.getWritableDatabase(); switch (uriMatcher.match(uri)) { case BOOK_DIR: case BOOK_ITEM: long newBookId = db.insert("Book", null, values); uriReturn = Uri.parse("content://" + AUTHORITY + "/book/" + newBookId); break; case CATEGORY_DIR: case CATEGORY_ITEM: long newCategoryId = db.insert("Category", null, values); uriReturn = Uri.parse("content://" + AUTHORITY + "/category/" + newCategoryId); break; default: break; } return uriReturn; }
@Override public Uri insert(Uri uri, ContentValues values) { // TODO Auto-generated method stub SQLiteDatabase database = dbHelper.getWritableDatabase(); Uri uriReturn = null; switch (uriMatcher.match(uri)) { case XidianData: case XidianDataItem: Long dataId = database.insert("XidianData", null, values); uriReturn = Uri.parse("content://wang.interfacedemo.provider/xidiandata/" + dataId); return uriReturn; case XidianStudentData: Long stuId = database.insert("XidianStudentData", null, values); uriReturn = Uri.parse("content://wang.interfacedemo.provider/xidianstudentdata/stuId" + stuId); return uriReturn; default: return uriReturn; } }
@Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO Auto-generated method stub int updateRows = 0; SQLiteDatabase database = dbHelper.getWritableDatabase(); switch (uriMatcher.match(uri)) { case XidianData: updateRows = database.update("XidianData", values, selection, selectionArgs); return updateRows; case XidianDataItem: String dataId = uri.getPathSegments().get(1); updateRows = database.update("XidianData", values, "id=?", new String[] {dataId}); return updateRows; case XidianStudentData: updateRows = database.update("XidianStudentData", values, selection, selectionArgs); return updateRows; default: return updateRows; } }
@Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { Log.d(TAG, "update uri " + uri); int rowsUpdated = 0; int uriType = sURIMatcher.match(uri); String id; SQLiteDatabase sqlDB = database.getWritableDatabase(); switch (uriType) { case EVENTS: rowsUpdated = sqlDB.update(TABLE_EVENTS, values, selection, selectionArgs); break; case RECORDS: rowsUpdated = sqlDB.update(TABLE_RECORDS, values, selection, selectionArgs); break; case RECORD_ID: id = uri.getLastPathSegment(); rowsUpdated = sqlDB.update(TABLE_RECORDS, values, Record.COL_ID + "=" + id, null); break; case EMPLOYEE_ID: id = uri.getLastPathSegment(); rowsUpdated = sqlDB.update(TABLE_EMPLOYEES, values, Employee.COL_ID + "=" + id, null); break; case EMPLOYEES: rowsUpdated = sqlDB.update(TABLE_EMPLOYEES, values, selection, selectionArgs); if (rowsUpdated > 0) uri = Uri.withAppendedPath(uri, selectionArgs[0]); break; case EVENT_ID: id = uri.getLastPathSegment(); rowsUpdated = sqlDB.update(TABLE_EVENTS, values, Event.COL_ID + "=" + id, null); break; default: break; } getContext().getContentResolver().notifyChange(uri, null); return rowsUpdated; }
@Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = dbHelper.getWritableDatabase(); int deletedRows = 0; switch (uriMatcher.match(uri)) { case BOOK_DIR: deletedRows = db.delete("Book", selection, selectionArgs); break; case BOOK_ITEM: String bookId = uri.getPathSegments().get(1); deletedRows = db.delete("Book", "id = ?", new String[] {bookId}); break; case CATEGORY_DIR: deletedRows = db.delete("Category", selection, selectionArgs); break; case CATEGORY_ITEM: String categoryId = uri.getPathSegments().get(1); deletedRows = db.delete("Category", "id = ?", new String[] {categoryId}); break; default: break; } return deletedRows; }
public void close() { DBHelper.close(); } // end close
public SampleDBManager openReadable() { db = DBHelper.getReadableDatabase(); return this; } // end openReadable