今天看了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. }

更多相关文章

  1. SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
  2. 使用Android(安卓)BroadcastReceiver应该注意的一些问题
  3. android studio *.jar 与 *.aar 的生成与*.aar导入项目方法
  4. Android的super.onCreate
  5. android 开发 Activity 与intent
  6. android之SQLite数据库应用(一)
  7. Android(安卓)Studio 快捷键整理
  8. Android之ContentProvider(数据共享)
  9. #android#数据持久化--SharedPreferences存储

随机推荐

  1. Android教程之如何使用自定义字体
  2. 不同android 版本的webview底层实现有差
  3. Android下载apk文件并安装
  4. Android(安卓)设计模式 之 观察者模式
  5. Android学习笔记_4_单元测试
  6. Android(安卓)recovery 下恢复备份文件
  7. AndroidManifest.xml文件详解(instrumenta
  8. Android(安卓)Studio 1.2 编码问题
  9. listview 中Item中含有Button 等造成Item
  10. Android(安卓)NDK r6 windows ,Cygwin 1.