Android ContentProvider的常用的增、刪、改、查使用方法
1、insert
public void insertEmergency(String name, String number, String msg, String account) {ContentResolver resolver = mContext.getContentResolver();
ContentValues initialValues = new ContentValues();
initialValues.put(EmergencyContactTable.EMERG_NAME, name);
initialValues.put(EmergencyContactTable.EMERG_NUMBER, number);
initialValues.put(EmergencyContactTable.EMERG_MSG, msg);
initialValues.put(EmergencyContactTable.EMERG_ACCOUNT, account);
resolver.insert(EmergencyContactTable.CONTENT_URI, initialValues);
}
2、delete
public void deleteEmergency(long rowId) {
ContentResolver resolver = mContext.getContentResolver();
resolver.delete(EmergencyContactTable.CONTENT_URI, _ID + "=?", new String[]{ String.valueOf(rowId) }) ;
}
3、update
public void updateEmergency(long rowId, String name, String number, String msg, String account) {
ContentResolver resolver = mContext.getContentResolver();
ContentValues initialValues = new ContentValues();
initialValues.put(EmergencyContactTable.EMERG_NAME, name);
initialValues.put(EmergencyContactTable.EMERG_NUMBER, number);
initialValues.put(EmergencyContactTable.EMERG_MSG, msg);
initialValues.put(EmergencyContactTable.EMERG_ACCOUNT, account);
resolver.update(EmergencyContactTable.CONTENT_URI, initialValues, _ID + "=?",new String[]{String.valueOf(rowId)}) ;
}
4、query
public Cursor getEmergency(long rowId) throws SQLException {
ContentResolver resolver = mContext.getContentResolver();
Cursor cursor = resolver.query(EmergencyContactTable.CONTENT_URI, null, _ID + "=?", new String[]{String.valueOf(rowId)}, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}