今天看了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()

例如:

import android.provider.Contacts.People; import android.content.ContentUris; import android.net.Uri; import android.database.Cursor; // Use the ContentUris method to produce the base URI for the contact with _ID == 23. Uri myPerson = ContentUris.withAppendedId(People.CONTENT_URI, 23); // Alternatively, use the Uri method to produce the base URI. // It takes a string rather than an integer. Uri myPerson = Uri.withAppendedPath(People.CONTENT_URI, "23"); // Then query for this specific record: 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 — _IDNUMBERNUMBER_KEYNAME, 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.)

括号里面的这个功能很方便哟,我有试成功

 

 

具备了以上的基础之后,我写了一个例子来实现排序和模糊查找(需要使用正则表达式),不过我一直很想做的多表查询始终无果,有高手知道的麻烦告诉我一下吧。

代码如下:

package com.ianc.querycontact; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentUris; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.Contacts; import android.util.Log; public class QueryContact extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Uri uri = Contacts.People.CONTENT_URI; String[] projection = {Contacts.People._ID, Contacts.People.PRIMARY_PHONE_ID, Contacts.PeopleColumns.NAME, Contacts.PeopleColumns.TIMES_CONTACTED}; String selection = Contacts.PeopleColumns.NAME + " like ?"; String[] selectionArgs = {"%li,%"}; String sortOrder = Contacts.PeopleColumns.NAME+" ASC"; Cursor cursor = managedQuery(uri, projection, selection, selectionArgs, sortOrder); int nColumnIndex; String id; int phoneID; String name; String times; ContentResolver cr = getContentResolver(); if(cursor.moveToFirst()){ Log.i("lily", "total "+cursor.getCount()+" records."); do { Log.i("lily", "***************************************"); nColumnIndex = cursor.getColumnIndex(Contacts.People._ID); id = cursor.getString(nColumnIndex); Log.i("lily", "id = " + id); nColumnIndex = cursor.getColumnIndex(Contacts.People.PRIMARY_PHONE_ID); phoneID = cursor.getInt(nColumnIndex); Log.i("lily", "phoneID = " + phoneID); Uri phoneuri = ContentUris.withAppendedId(Contacts.Phones.CONTENT_URI, phoneID); String[] phoneprojection = {Contacts.Phones._ID, Contacts.PhonesColumns.NUMBER}; Cursor phonecursor = cr.query(phoneuri, phoneprojection, null, null, null); if (phonecursor.moveToFirst()){ String phoneNumber = phonecursor.getString(phonecursor.getColumnIndex(Contacts.PhonesColumns.NUMBER)); Log.i("lily", "phoneNumber = " + phoneNumber); }else{ Log.i("lily", "no phone number"); } phonecursor.close(); nColumnIndex = cursor.getColumnIndex(Contacts.PeopleColumns.NAME); name = cursor.getString(nColumnIndex); Log.i("lily", "name = "+ name); nColumnIndex = cursor.getColumnIndex(Contacts.PeopleColumns.TIMES_CONTACTED); times = cursor.getString(nColumnIndex); Log.i("lily", "contact times = "+times); Log.i("lily", "***************************************"); }while(cursor.moveToNext()); }else{ Log.i("lily", "no result"); } cursor.close(); } @Override protected void onResume() { super.onResume(); } }

最后附上一些我参考的网址:

Content Provider基础之SQL  http://notfatboy.javaeye.com/blog/653357

模糊查找 再深入 http://griffinshi.javaeye.com/blog/666875

Android 数据存取之Databases http://hi.baidu.com/_java/blog/item/f59a921cb633ec8387d6b6ed.html

还有SQLite的官方网站http://www.sqlite.org/optoverview.html#where_clause

更多相关文章

  1. SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
  2. Android中进入页面默认定位到ListView的第一条数据解决方法
  3. Android日常开发(18)android 调用 vue methods 方法,提示"Uncaught
  4. android TextView空间的setTextSize()方法在真机上运行大小问题
  5. Android:onNewIntent的使用
  6. android中httpclient和HttpURLConnection优缺点和常见bug解决方
  7. 全局获取Context的技巧(再也不要为获取Context而感到烦恼)
  8. 梳理Android的IPC进程间通信(最新AndroidStudio的AIDL操作)
  9. Android(安卓)studio 快捷键小计

随机推荐

  1. android 录像和拍照功能
  2. Android的数据库--sqlite(一)
  3. Android(安卓)ListView 几个重要属性
  4. android:Kotlin Java Kotlin android Kot
  5. Android(安卓)项目基础之XML
  6. android 之PopupWindow的使用
  7. 2011.07.06(2)——— android apiDemos 之
  8. Unity—Android通讯
  9. [置顶] Android(安卓)百度地图 简单实现-
  10. CheckedTextView 显示问题