Code example for ContentResolver

Methods: insertqueryupdate

0
		Log.i(TAG, "setPreference");
		try { 
			Log.i(TAG, "get Cursor.");
			if (mContentResolver == null)
				Log.i(TAG, "Panic!.");
			Cursor c = mContentResolver.query(Preferences.CONTENT_URI,
					mProjectionPreferencesFilter, Preferences.NAME + "= '"
							+ name + "'", null, Preferences.DEFAULT_SORT_ORDER);
			Log.i(TAG, "got Cursor.");
			// Log.i(TAG, "Cursor: " + c.toString()); 
 
			if (c == null) {
				Log.e(TAG, "missing hardware provider");
				return; 
			} 
 
			if (c == null || c.getCount() < 1) {
				Log.i(TAG, "Insert");
 
				// This value does not exist yet. Let's insert it: 
				ContentValues values = new ContentValues(2);
				values.put(Preferences.NAME, name);
				values.put(Preferences.VALUE, value);
				mContentResolver.insert(Preferences.CONTENT_URI, values);
			} else if (c.getCount() >= 1) {
				Log.i(TAG, "Update");
 
				// This is the key, so we can update it: 
				c.moveToFirst();
				String id = c.getString(mProjectionPreferencesID);
				ContentValues cv = new ContentValues();
				cv.put(Preferences.VALUE, value);
				mContentResolver.update(
						Uri.withAppendedPath(Preferences.CONTENT_URI, id), cv,
						null, null); 
 
				// c.requery(); 
				c.getString(mProjectionPreferencesVALUE);