ContentProvider-4多表

我的理解:其实ContentProvider的机制很随意,它就类似于一个服务器一样,你把uri传来,只要按照特定的方式,它就能给你特定的功能,我觉得这个机制自由又方便。
其实这两个功能主要通过修改query就可以,完整的ContentProvider代码如下:


view plaincopy to clipboardprint?
1. package com.ianc.lilyprovider;  
2. import android.content.ContentProvider;  
3. import android.content.ContentValues;  
4. import android.content.Context;  
5. import android.content.UriMatcher;  
6. import android.database.Cursor;  
7. import android.database.sqlite.SQLiteDatabase;  
8. import android.database.sqlite.SQLiteOpenHelper;  
9. import android.net.Uri;  
10. import android.util.Log;  
11. public class LilyProvider extends ContentProvider {  
12.     final static String TABLE_NAME = "customer";  
13.     final static String PRODUCT_TABLE = "shoplist";  
14.     private static final String  DATABASE_NAME = "lily.db";  
15.     private static String AUTHORITY = "com.ianc.lilyprovider";  
16.     private static final int DATABASE_VERSION = 1;  
17.       
18.     DatabaseHelper mDbHelper;  
19.     static UriMatcher sUriMatcher;  
20.     private static final int USER = 1;  
21.     private static final int USER_NAME = 2;  
22.     private static final int MULITABLE = 3;  
23.     private static final int SHOPLIST = 4;  

// 传入匹配码如果大于0表示匹配根路径或传入-1,即常量UriMatcher.NO_MATCH表示不匹配根路径

// addURI()方法是用来增加其他URI匹配路径的:

// 第一个参数代表传入标识ContentProvider的AUTHORITY字符串

// 第二个参数是要匹配的路径,#代表任意数字,另外还可以用*来匹配任意文本

// 第三个参数必须传入一个大于零的匹配码,用于match()方法对相匹配的URI返回相对应的匹配码

24.     static{  
25.         sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);  
26.         sUriMatcher.addURI(AUTHORITY, TABLE_NAME, USER);  
27.         sUriMatcher.addURI(AUTHORITY, PRODUCT_TABLE, SHOPLIST);  
28.         sUriMatcher.addURI(AUTHORITY, TABLE_NAME+"/"+LilyUser.UserColumns.NAME+"/*", USER_NAME);//search special user by name  
29.         sUriMatcher.addURI(AUTHORITY, TABLE_NAME+"/*", MULITABLE);  
30.     }  
31. //内部类
32.     class DatabaseHelper extends SQLiteOpenHelper {  
33.         public DatabaseHelper(Context context) {  
34.             super(context, DATABASE_NAME, null, DATABASE_VERSION);  
35.         }  
36.         @Override  
37.         public void onCreate(SQLiteDatabase db) {  
38.             Log.i("lily","LilyProvider-->DatabaseHelper-->onCreate");  
39.             db.execSQL("Create table " + TABLE_NAME + "( " +  
40.                     LilyUser.UserColumns._ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, " +  
41.                     LilyUser.UserColumns._COUNT + " INTEGER,"+  
42.                     LilyUser.UserColumns.NAME + " TEXT," +  
43.                     LilyUser.UserColumns.PASSWORD +" TEXT" +  
44.                     ");");  
45.             db.execSQL("Create table " + PRODUCT_TABLE + "( " +  
46.                     LilyUser.ShopListColumns._ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, " +  
47.                     LilyUser.ShopListColumns._COUNT + " INTEGER,"+  
48.                     LilyUser.ShopListColumns.USER_ID+" INTEGER,"+  
49.                     LilyUser.ShopListColumns.PRODUCT + " TEXT," +  
50.                     LilyUser.ShopListColumns.DATE +" TEXT" +  
51.                     ");");  
52.         }  
53.         @Override  
54.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
55.             Log.i("lily","LilyProvider-->DatabaseHelper-->onUpgrade");  
56.             db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME+";");  
57.             db.execSQL("DROP TABLE IF EXISTS "+PRODUCT_TABLE+";");  
58.             onCreate(db);  
59.         }  
60.           
61.     }  
62.     @Override  
63.     public int delete(Uri arg0, String arg1, String[] arg2) {  
64.         Log.i("lily","LilyProvider-->delete");  
65.         SQLiteDatabase db = mDbHelper.getWritableDatabase();  
66.         int rownum = db.delete(TABLE_NAME, arg1, arg2);  
67.         return rownum;  
68.     }  
69.     @Override  
70.     public String getType(Uri uri) {  
71.         Log.i("lily","LilyProvider-->getType");  
72.         return null;  
73.     }  
74.     @Override  
75.     public Uri insert(Uri uri, ContentValues values) {  
76.         Log.i("lily","LilyProvider-->insert");  
77.         if (sUriMatcher.match(uri) == USER){  
78.             SQLiteDatabase db = mDbHelper.getWritableDatabase();  
79.             db.insert(TABLE_NAME, null, values);  
80.         }  
81.         else if (sUriMatcher.match(uri) == SHOPLIST){  
82.             SQLiteDatabase db = mDbHelper.getWritableDatabase();  
83.             db.insert(PRODUCT_TABLE, null, values);  
84.         }  
85.       
86.         return null;  
87.     }  
88.     @Override  
89.     public boolean onCreate() {  
90.         Log.i("lily","LilyProvider-->onCreate");  
91.         mDbHelper = new DatabaseHelper(this.getContext());  
92.         return false;  
93.     }  
94.     @Override  
95.     public Cursor query(Uri uri, String[] projection, String selection,  
96.             String[] selectionArgs, String sortOrder) {  
97.         Log.i("lily","LilyProvider-->query");  
98.         Log.i("lily","uri = "+uri);  
99.         Log.i("lily","sUriMatcher.match(uri)="+sUriMatcher.match(uri));  
100.         Cursor c = null;  
101.         if (sUriMatcher.match(uri) == USER){  
102.             SQLiteDatabase db = mDbHelper.getReadableDatabase();  
103.             c = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);  
104.         }else if (sUriMatcher.match(uri) == USER_NAME){  
105.               
106.             // format:authority/table/column/value-->0:table 1:column 2:value  
107.             String name = uri.getPathSegments().get(2);// get name value  
108.             Log.i("lily","query table:"+uri.getPathSegments().get(0)+" column:"+uri.getPathSegments().get(1)+" value:"+uri.getPathSegments().get(2));  
109.             SQLiteDatabase db = mDbHelper.getReadableDatabase();  
110.             if (selection != null){  
111.                 if (selection.length()>0){  
112.                     selection += "AND "+LilyUser.UserColumns.NAME+" like "+name;  
113.                 }  
114.             }  
115.             c = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);  
116.         }else if (sUriMatcher.match(uri) == MULITABLE){  
117.             // format:authority/table1/table2  *******
118.             String table1 = uri.getPathSegments().get(0);  
119.             String table2 = uri.getPathSegments().get(1);  
120.               
121.             Log.i("lily","table1 = "+table1);  
122.             Log.i("lily","table2 = "+table2);  
123.               
124.             if (table1.equalsIgnoreCase(TABLE_NAME)){  
125.                 if (table2.equals(PRODUCT_TABLE)){  
126.                     SQLiteDatabase db = mDbHelper.getReadableDatabase();  
127.                     if (selection != null){  
128.                         if (selection.length()>0){  
129.                             selection += "AND "+LilyUser.UserColumns._ID+" = "+LilyUser.ShopListColumns.USER_ID;  
130.                         }  
131.                         else{  
132.                             selection = LilyUser.UserColumns._ID+" = "+LilyUser.ShopListColumns.USER_ID;  
133.                         }     
134.                     }  
135.                     else  
136.                     {  
137.                         selection = TABLE_NAME + "." +LilyUser.UserColumns._ID+" = "+PRODUCT_TABLE + "." + LilyUser.ShopListColumns.USER_ID;  
138.                     }  
139.                     c = db.query(table1+" cross join "+table2, projection, null, selectionArgs, null, null, sortOrder);  
140.                 }  
141.             }  
142.         }  
143.       
144.         return c;  
145.     }  
146.     @Override  
147.     public int update(Uri uri, ContentValues values, String selection,  
148.             String[] selectionArgs) {  
149.         Log.i("lily","LilyProvider-->update");  
150.         SQLiteDatabase db = mDbHelper.getWritableDatabase();  
151.         int rownum = db.update(TABLE_NAME, values, selection, selectionArgs);  
152.         return rownum;  
153.     }  
154. }  
 
这个里面定义Uri格式的时候一定要小心,不然出错了死都查不出来。
下面是使用查询的方法:


view plaincopy to clipboardprint?
1. private void queryNameUri() {  
2.         Uri uri = Uri.withAppendedPath(LilyUser.User.CONTENT_URI, "name/lily");//content://com.ianc.lilyprovider/customer/name/lily  
3.         String[] projection = {LilyUser.UserColumns._ID, LilyUser.UserColumns.NAME, LilyUser.UserColumns.PASSWORD};  
4.         String selection = null;  
5.         String [] selectionArgs = null;  
6.         String sortOrder = null;  
7.         Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);  
8.         if (cursor == null){  
9.             Log.i("lily","cursor == null");  
10.             return;  
11.         }  
12.         if (cursor.moveToFirst()){  
13.             Log.i("lily","*********************************************");  
14.             String id;  
15.             String name;  
16.             String password;  
17.             do{  
18.                 id = cursor.getString(cursor.getColumnIndex(LilyUser.UserColumns._ID));  
19.                 Log.i("lily","id = "+id);  
20.                   
21.                 name = cursor.getString(cursor.getColumnIndex(LilyUser.UserColumns.NAME));  
22.                 Log.i("lily","name = "+name);  
23.                   
24.                 password = cursor.getString(cursor.getColumnIndex(LilyUser.UserColumns.PASSWORD));  
25.                 Log.i("lily","password = "+password);  
26.                   
27.                 Log.i("lily","*********************************************");  
28.             }while(cursor.moveToNext());  
29.         }  
30.         else{  
31.             Log.i("lily","no result");  
32.         }  
33.         cursor.close();  
34.     }  
 多表查询:


view plaincopy to clipboardprint?
1. private void queryMultiTable() {  
2.         String[] projection = {"customer."+LilyUser.UserColumns._ID, "customer."+LilyUser.UserColumns.NAME, "customer."+LilyUser.UserColumns.PASSWORD,   
3.                 "shoplist."+LilyUser.ShopListColumns.USER_ID, "shoplist."+LilyUser.ShopListColumns.PRODUCT, "shoplist."+LilyUser.ShopListColumns.DATE};  
4.         String selection = null;  
5.         String [] selectionArgs = null;  
6.         String sortOrder = null;  
7.         Uri uri = Uri.withAppendedPath(LilyUser.User.CONTENT_URI, "shoplist");  
8.         Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);  
9.         if (cursor == null){  
10.             Log.i("lily","queryMultiTable()-->cursor == null");  
11.             return;  
12.         }  
13.         if (cursor.moveToFirst()){  
14.             Log.i("lily","*********************************************");  
15.             String id;  
16.             String name;  
17.             String password;  
18.             String user_id;  
19.             String product;  
20.             String date;  
21.             do{  
22.                 id = cursor.getString(cursor.getColumnIndex(LilyUser.UserColumns._ID));  
23.                 Log.i("lily","id = "+id);  
24.                   
25.                 name = cursor.getString(cursor.getColumnIndex(LilyUser.UserColumns.NAME));  
26.                 Log.i("lily","name = "+name);  
27.                   
28.                 password = cursor.getString(cursor.getColumnIndex(LilyUser.UserColumns.PASSWORD));  
29.                 Log.i("lily","password = "+password);  
30.                   
31.                 user_id = cursor.getString(cursor.getColumnIndex(LilyUser.ShopListColumns.USER_ID));  
32.                 Log.i("lily","user_id = "+user_id);  
33.                   
34.                 product = cursor.getString(cursor.getColumnIndex(LilyUser.ShopListColumns.PRODUCT));  
35.                 Log.i("lily","product = " + product);  
36.                   
37.                 date = cursor.getString(cursor.getColumnIndex(LilyUser.ShopListColumns.DATE));  
38.                 Log.i("lily","date = " + date);  
39.                   
40.                 Log.i("lily","*********************************************");  
41.             }while(cursor.moveToNext());  
42.         }  
43.         else{  
44.             Log.i("lily","no result");  
45.         }  
46.         cursor.close();  
47.     }  
 接下来是LilyUser这个说明类的代码:


view plaincopy to clipboardprint?
1. package com.ianc.lilyprovider;  
2. import android.net.Uri;  
3. import android.provider.BaseColumns;  
4. public class LilyUser {  
5.       
6.     public static class User{  
7.         public static final Uri CONTENT_URI = Uri.parse("content://com.ianc.lilyprovider/" + "customer");  
8.     }  
9.     public static class UserColumns implements BaseColumns{   
10.         public static String NAME = "name";  
11.         public static String PASSWORD = "password";  
12.     }  
13.       
14.     public static class ShopList{  
15.         public static final Uri CONTENT_URI = Uri.parse("content://com.ianc.lilyprovider/" + "shoplist");  
16.     }  
17.     public static class ShopListColumns implements BaseColumns{   
18.         public static String USER_ID = "user_id";  
19.         public static String PRODUCT = "product";  
20.         public static String DATE = "date";  
21.     }  
22. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值