ContentProviderの学習---1つ:データベースのクエリー
6269 ワード
今日androidの公式ドキュメントのContentProviderの部分を見て、データベースの使用は私がずっとぼんやりしているため、私は自分でproviderを書きたいと思って、更に1つの工事を書いてそれを使ってデータを読んで、データを建てて、だから今日先にどのように検索するこの部分の知識を学んで、まずいくつか公式ドキュメントの中からまとめたいくつかの点です:
1.クエリーに必要な3つの条件:
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.クエリーには2つの方法があります.
1. ContentResolver.query()
2. Activity.M a n agedQuery()
もちろん、データベースをクエリーする方法はこの2つだけではありません.例えば、SQLiteDatabaseのqueryメソッドを使用すると、より複雑な検索が可能になります.
3.IDを知っている場合は、このようにデータベースを調べることができます.
ContentUrisを使用する.それは...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 A filter detailing which rows to return, formatted as an SQL Selection arguments. A sorting order for the rows that are returned, formatted as an SQL
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(); } }
最後に参考にしたURLを添付します.
Content ProviderベースSQLhttp://notfatboy.javaeye.com/blog/653357
ファジイ検索http://griffinshi.javaeye.com/blog/666875
AndroidデータアクセスのDatabaseshttp://hi.baidu.com/_java/blog/item/f59a921cb633ec8387d6b6ed.html
SQLiteの公式サイトもありますhttp://www.sqlite.org/optoverview.html#where_clause
1.クエリーに必要な3つの条件:
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.クエリーには2つの方法があります.
1. ContentResolver.query()
2. Activity.M a n agedQuery()
:
unloading itself when the activity pauses,and requerying itself when the activity restarts,Activity.startManagingCursor()を使用して開始manageを制御し、stopManagingCursor(Cursor c)を使用して終了manageを制御します.もちろん、データベースをクエリーする方法はこの2つだけではありません.例えば、SQLiteDatabaseのqueryメソッドを使用すると、より複雑な検索が可能になります.
3.IDを知っている場合は、このようにデータベースを調べることができます.
ContentUrisを使用する.それは...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.その他のパラメータの説明
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. WHERE
clause (excluding the WHERE
itself). A null
value returns all rows (unless the URI limits the query to a single record). 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(); } }
最後に参考にしたURLを添付します.
Content ProviderベースSQLhttp://notfatboy.javaeye.com/blog/653357
ファジイ検索http://griffinshi.javaeye.com/blog/666875
AndroidデータアクセスのDatabaseshttp://hi.baidu.com/_java/blog/item/f59a921cb633ec8387d6b6ed.html
SQLiteの公式サイトもありますhttp://www.sqlite.org/optoverview.html#where_clause