ContentProvider-1查询

今天看了android的官方文档中ContentProvider的那部分,因为数据库使用我一直很晕乎,我想要完成自己写一个provider,再写一个工程来使用它读数据,建数据,所以今天先学习了如何查询的这部分知识,首先是一些从官方文档中总结出来的几点:
1.查询必备的三个条件:
1.The URI that identifies the provider-->URI
2.The names of the data fields you want to receive-->data fields
3.The data types for those fields-->data types
所以写Provider的时候也必须要提供一个类,来把这些数据暴露给使用者们
 
 
2.查询有两种方法:
1. ContentResolver.query() 
2. Activity.managedQuery():unloading itself when the activity pauses, and requerying itself when the activity restarts,可以使用Activity.startManagingCursor()来控制开始manage和使用stopManagingCursor(Cursor c)结束manage
 
当然查询数据库的方法肯定不止这两个,比如使用SQLiteDatabase的query方法,可以有更多复杂的查法。
 
3.如果你已知ID的情况下,可以这么查数据库
使用ContentUris.withAppendedId() 或 Uri.withAppendedPath()
例如:


view plaincopy to clipboardprint?
1. import android.provider.Contacts.People;  
2. import android.content.ContentUris;  
3. import android.net.Uri;  
4. import android.database.Cursor;  
5.   
6. // Use the ContentUris method to produce the base URI for the contact with _ID == 23.  
7. Uri myPerson = ContentUris.withAppendedId(People.CONTENT_URI, 23);  
8.   
9. // Alternatively, use the Uri method to produce the base URI.  
10. // It takes a string rather than an integer.  
11. Uri myPerson = Uri.withAppendedPath(People.CONTENT_URI, "23");  
12.   
13. // Then query for this specific record:  
14. Cursor cur = managedQuery(myPerson, null, null, null, null);  
4.其他参数说明
• The names of the data columns that should be returned. A null value returns all columns. Otherwise, only columns that are listed by name are returned. All the content providers that come with the platform define constants for their columns. For example, the android.provider.Contacts.Phones class defines constants for the names of the columns in the phone table illustrated earlier — _ID, NUMBER, NUMBER_KEY, NAME, and so on.
• A filter detailing which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). A null value returns all rows (unless the URI limits the query to a single record).
• Selection arguments.
• A sorting order for the rows that are returned, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). A null value returns the records in the default order for the table, which may be unordered
5.取得查询结果

1. The Cursor lets you request the column name from the index of the column, or the index number from the column name.
(可以从从column name拿到column index,反之也可以从column index拿到column name)
2. Cursor object has a separate method for reading each type of data — such as getString(), getInt(), and getFloat(). 
(However, for most types, if you call the method for reading strings, the Cursor object will give you the String representation of the data.)
括号里面的这个功能很方便哟,我有试成功
 
 
具备了以上的基础之后,我写了一个例子来实现排序和模糊查找(需要使用正则表达式),不过我一直很想做的多表查询始终无果,有高手知道的麻烦告诉我一下吧。
代码如下:


view plaincopy to clipboardprint?
1. package com.ianc.querycontact;  
2.   
3. import android.app.Activity;  
4. import android.content.ContentResolver;  
5. import android.content.ContentUris;  
6. import android.database.Cursor;  
7. import android.net.Uri;  
8. import android.os.Bundle;  
9. import android.provider.Contacts;  
10. import android.util.Log;  
11.   
12. public class QueryContact extends Activity {  
13.     /** Called when the activity is first created. */  
14.     @Override  
15.     public void onCreate(Bundle savedInstanceState) {  
16.         super.onCreate(savedInstanceState);  
17.         setContentView(R.layout.main);  
18.         Uri uri = Contacts.People.CONTENT_URI;  
19.         String[] projection = {Contacts.People._ID, Contacts.People.PRIMARY_PHONE_ID, Contacts.PeopleColumns.NAME, Contacts.PeopleColumns.TIMES_CONTACTED};  
20.         String selection = Contacts.PeopleColumns.NAME + " like ?";  
21.         String[] selectionArgs = {"%li,%"};  
22.         String sortOrder = Contacts.PeopleColumns.NAME+" ASC";  
23.         Cursor cursor = managedQuery(uri, projection, selection, selectionArgs, sortOrder);  
24.         int nColumnIndex;  
25.         String id;  
26.         int phoneID;  
27.         String name;  
28.         String times;  
29.         ContentResolver cr = getContentResolver();  
30.         if(cursor.moveToFirst()){  
31.               
32.             Log.i("lily", "total "+cursor.getCount()+" records.");  
33.               
34.             do {  
35.                 Log.i("lily", "***************************************");  
36.                   
37.                 nColumnIndex = cursor.getColumnIndex(Contacts.People._ID);  
38.                 id = cursor.getString(nColumnIndex);  
39.                 Log.i("lily", "id = " + id);  
40.                   
41.                 nColumnIndex = cursor.getColumnIndex(Contacts.People.PRIMARY_PHONE_ID);  
42.                 phoneID = cursor.getInt(nColumnIndex);  
43.                 Log.i("lily", "phoneID = " + phoneID);  
44.                 Uri phoneuri = ContentUris.withAppendedId(Contacts.Phones.CONTENT_URI, phoneID);  
45.                 String[] phoneprojection = {Contacts.Phones._ID, Contacts.PhonesColumns.NUMBER};  
46.                 Cursor phonecursor = cr.query(phoneuri, phoneprojection, null, null, null);  
47.                 if (phonecursor.moveToFirst()){  
48.                     String phoneNumber = phonecursor.getString(phonecursor.getColumnIndex(Contacts.PhonesColumns.NUMBER));  
49.                     Log.i("lily", "phoneNumber = " + phoneNumber);  
50.                 }else{  
51.                     Log.i("lily", "no phone number");  
52.                 }  
53.                 phonecursor.close();  
54.                   
55.                   
56.                 nColumnIndex = cursor.getColumnIndex(Contacts.PeopleColumns.NAME);  
57.                 name = cursor.getString(nColumnIndex);  
58.                 Log.i("lily", "name = "+ name);  
59.                   
60.                 nColumnIndex = cursor.getColumnIndex(Contacts.PeopleColumns.TIMES_CONTACTED);  
61.                 times = cursor.getString(nColumnIndex);  
62.                 Log.i("lily", "contact times = "+times);  
63.                   
64.                 Log.i("lily", "***************************************");  
65.             }while(cursor.moveToNext());  
66.         }else{  
67.             Log.i("lily", "no result");  
68.         }  
69.         cursor.close();  
70.     }  
71.   
72.     @Override  
73.     protected void onResume() {  
74.           
75.         super.onResume();  
76.     }  
77.       
78. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值