ContentProivde 内容提供者,内部封装了对数据库的代码操作过程,对外暴露几个方法,可以提供给其他应用程序对本程序的数据库进行操作的的方法。
public class Constant {
public static final String AUTHORITIES="com.example.emma.demo0504.provider.peoplecontentprovider";
public static final String TABLENAME="t_person";
public static final String TABLE_PERSON_ID="_id";
public static final String TABLE_PERSON_NAME="name";
public static final String TABLE_PERSON_AGE="age";
public static final String TABLE_PERSON_ADDR="addr";
}
public class PeopleContentProvider extends ContentProvider {
private Bean bean;
private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int MULTITUDE = 2;
private static final int SINGLE = 1;
static {
uriMatcher.addURI(Constant.AUTHORITIES, "/people", MULTITUDE);
uriMatcher.addURI(Constant.AUTHORITIES, "/people/#", SINGLE);
}
@Override
public boolean onCreate() {
bean = Bean.getInstance(getContext());
return true;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
int matchCode = uriMatcher.match(uri);
switch (matchCode) {
case MULTITUDE:
return bean.select(projection, selection, selectionArgs, sortOrder);
case SINGLE:
long id = ContentUris.parseId(uri);
if (selection == null) {
return bean.select(projection, "_id=" + id, selectionArgs, sortOrder);
} else {
return bean.select(projection, selection + " AND _id=" + id, selectionArgs, sortOrder);
}
default:
throw new RuntimeException("非法Uri");
}
}
@Nullable
@Override
public String getType(Uri uri) {
int matchCode = uriMatcher.match(uri);
switch (matchCode) {
case MULTITUDE:
return "vnd.android.cursor/dir";
case SINGLE:
return "vnd.android.cursor/item";
default:
throw new RuntimeException("非法Uri");
}
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
int matchCode = uriMatcher.match(uri);
switch (matchCode) {
case MULTITUDE:
long id = bean.insert(values);
return Uri.withAppendedPath(uri, String.valueOf(id));
default:
throw new RuntimeException("非法Uri");
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int matchCode = uriMatcher.match(uri);
switch (matchCode) {
case MULTITUDE:
return bean.delete(selection, selectionArgs);
case SINGLE:
long id = ContentUris.parseId(uri);
if (selection == null) {
return bean.delete("_id=" + id, selectionArgs);
} else {
return bean.delete(selection + " AND _id=" + id, selectionArgs);
}
default:
throw new RuntimeException("非法Uri");
}
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int matchCode = uriMatcher.match(uri);
switch (matchCode) {
case SINGLE:
return bean.update(values, selection, selectionArgs);
default:
throw new RuntimeException("非法Uri");
}
}
}
注册Provider
最后一句表示授予其他程序访问的权限
<provider
android:name=".provider.PeopleContentProvider"
android:authorities="com.example.emma.demo0504.provider.peoplecontentprovider"
android:exported="true"></provider>
操作数据库的类
public class Bean extends SQLiteOpenHelper {
private static Bean bean;
private static String DBNAME = "person.db";
private static int VERSION = 1;
private Bean(Context context) {
super(context, DBNAME, null, VERSION, null);
}
public static Bean getInstance(Context context) {
if (bean == null) {
bean = new Bean(context);
}
return bean;
}
@Override
public void onCreate(SQLiteDatabase db) {
StringBuffer creat = new StringBuffer();
creat.append("CREATE TABLE IF NOT EXISTS ")
.append(Constant.TABLENAME)
.append("(")
.append(Constant.TABLE_PERSON_ID)
.append(" INTEGER PRIMARY KEY AUTOINCREMENT,")
.append(Constant.TABLE_PERSON_NAME)
.append(" VARCHAR(200),")
.append(Constant.TABLE_PERSON_AGE)
.append(" INTEGER DEFAULT 1 CHECK (age BETWEEN 1 AND 150),")
.append(Constant.TABLE_PERSON_ADDR)
.append(" VARCHAR(200)")
.append(")");
db.execSQL(creat.toString());
StringBuffer insert = new StringBuffer();
insert.append("INSERT INTO ")
.append(Constant.TABLENAME)
.append("(")
.append(Constant.TABLE_PERSON_ID + ",")
.append(Constant.TABLE_PERSON_NAME + ",")
.append(Constant.TABLE_PERSON_AGE + ",")
.append(Constant.TABLE_PERSON_ADDR)
.append(")")
.append(" VALUES ")
.append("(null,?,?,?)");
db.execSQL(insert.toString(), new String[]{"赵", String.valueOf(21), "上海"});
db.execSQL(insert.toString(), new String[]{"钱", String.valueOf(22), "温州"});
db.execSQL(insert.toString(), new String[]{"孙", String.valueOf(23), "北京"});
db.execSQL(insert.toString(), new String[]{"李", String.valueOf(24), "杭州"});
db.execSQL(insert.toString(), new String[]{"周", String.valueOf(25), "丽水"});
db.execSQL(insert.toString(), new String[]{"吴", String.valueOf(26), "天津"});
db.execSQL(insert.toString(), new String[]{"郑", String.valueOf(27), "福州"});
db.execSQL(insert.toString(), new String[]{"王", String.valueOf(28), "莆田"});
db.execSQL(insert.toString(), new String[]{"冯", String.valueOf(29), "福鼎"});
db.execSQL(insert.toString(), new String[]{"陈", String.valueOf(30), "金华"});
db.execSQL(insert.toString(), new String[]{"楚", String.valueOf(31), "嘉兴"});
db.execSQL(insert.toString(), new String[]{"魏", String.valueOf(32), "广州"});
db.execSQL(insert.toString(), new String[]{"蒋", String.valueOf(33), "深圳"});
db.execSQL(insert.toString(), new String[]{"沈", String.valueOf(34), "香港"});
db.execSQL(insert.toString(), new String[]{"韩", String.valueOf(35), "澳门"});
db.execSQL(insert.toString(), new String[]{"杨", String.valueOf(36), "南京"});
}
public Cursor select(String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = null;
db = bean.getWritableDatabase();
return db.query(Constant.TABLENAME, projection, selection, selectionArgs, null, null, sortOrder);
}
public long insert(ContentValues values) {
SQLiteDatabase db = null;
try {
db = bean.getWritableDatabase();
return db.insert(Constant.TABLENAME, "hello", values);
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
if(db!=null&&db.isOpen()){
db.close();
db=null;
}
}
}
public int delete(String selection, String[] selectionArgs) {
SQLiteDatabase db = null;
try {
db = bean.getWritableDatabase();
return db.delete(Constant.TABLENAME, selection, selectionArgs);
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
if(db!=null&&db.isOpen()){
db.close();
db=null;
}
}
}
public int update(ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = null;
try {
db = bean.getWritableDatabase();
return db.update(Constant.TABLENAME,values,selection,selectionArgs);
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
if(db!=null&&db.isOpen()){
db.close();
db=null;
}
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}