1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
| public class PersonProvider extends ContentProvider {
private ItemDatabaseHelper helper;
private SQLiteDatabase db;
// private UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
@Override
public boolean onCreate() {
helper = new ItemDatabaseHelper(this.getContext());
// matcher.addURI("com.example.test", "person", 1);
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
return null;
}
/*
* 如果操作集合,则必须以vnd.android.cursor.dir开头 如果操作非集合,则必须以vnd.android.cursor.item开头
*/
@Override
public String getType(Uri uri) {
return "";
}
@Override
public Uri insert(Uri uri, ContentValues values) {
db = helper.getWritableDatabase();
long rowid = db.insert("person", null, values);
this.getContext().getContentResolver().notifyChange(uri, null);// 如果改变数据,则通知所有人
return ContentUris.withAppendedId(uri, rowid); // 返回插入的记录所代表的URI
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return 0;
}
private static class ItemDatabaseHelper extends SQLiteOpenHelper {
public ItemDatabaseHelper(Context context) {
super(context, "person", null, 1);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL("create table if not exists person("
+ " _id integer primary key autoincrement," + " name text,"
+ "cid text" + ");");
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {
database.execSQL("drop table if exists items");
onCreate(database);
}
}
}
|