実際には、3 つのクラスだけが必要です。
ここにあるContentProvider : http://developer.android.com/guide/topics/providers/content-providers.html
2 番目に必要なのはSQLiteOpenHelperで、最後にCursorが必要です
編集:スニペットからdb
変数が何であるかが明らかではないことに気付きました。それはSQLiteOpenHelperまたはそれの私の拡張です(onCreate、onUpgrade、およびコンストラクターのみをオーバーライドしました。以下を参照してください^^
ContentProvider は、データベースと通信し、挿入、更新、削除を行うものです。コンテンツ プロバイダーは、コードの他の部分 (許可する場合は他のアプリも含む) が sqlite に保存されているデータにアクセスできるようにします。
次に、挿入/削除/クエリ/更新関数をオーバーライドして、機能を追加できます。たとえば、インテントの URI に応じてさまざまなアクションを実行できます。
public int delete(Uri uri, String whereClause, String[] whereArgs) {
int count = 0;
switch(URI_MATCHER.match(uri)){
case ITEMS:
// uri = content://com.yourname.yourapp.Items/item
// delete all rows
count = db.delete(TABLE_ITEMS, whereClause, whereArgs);
break;
case ITEMS_ID:
// uri = content://com.yourname.yourapp.Items/item/2
// delete the row with the id 2
String segment = uri.getPathSegments().get(1);
count = db.delete(TABLE_ITEMS,
Item.KEY_ITEM_ID +"="+segment
+(!TextUtils.isEmpty(whereClause)?" AND ("+whereClause+")":""),
whereArgs);
break;
default:
throw new IllegalArgumentException("Unknown Uri: "+uri);
}
return count;
}
UriMatcher は次のように定義されます。
private static final int ITEMS = 1;
private static final int ITEMS_ID = 2;
private static final String AUTHORITY_ITEMS ="com.yourname.yourapp.Items";
private static final UriMatcher URI_MATCHER;
static {
URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
URI_MATCHER.addURI(AUTHORITY_ITEMS, "item", ITEMS);
URI_MATCHER.addURI(AUTHORITY_ITEMS, "item/#", ITEMS_ID);
}
このようにして、1 つの結果のみを返すか更新するか、またはすべてを照会するかどうかを決定できます。
SQLiteOpenHelper は実際に挿入を実行し、SQLite データベースの構造が変更された場合はアップグレードも処理します。オーバーライドすることでそこで実行できます。
class ItemDatabaseHelper extends SQLiteOpenHelper {
public ItemDatabaseHelper(Context context){
super(context, "myDatabase.db", null, ITEMDATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String createItemsTable = "create table " + TABLE_ITEMS + " (" +
...
");";
// Begin Transaction
db.beginTransaction();
try{
// Create Items table
db.execSQL(createItemsTable);
// Transaction was successful
db.setTransactionSuccessful();
} catch(Exception ex) {
Log.e(this.getClass().getName(), ex.getMessage(), ex);
} finally {
// End transaction
db.endTransaction();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String dropItemsTable = "DROP TABLE IF EXISTS " + TABLE_ITEMS;
// Begin transaction
db.beginTransaction();
try {
if(oldVersion<2){
// Upgrade from version 1 to version 2: DROP the whole table
db.execSQL(dropItemsTable);
onCreate(db);
Log.i(this.getClass().toString(),"Successfully upgraded to Version 2");
}
if(oldVersion<3) {
// minor change, perform an ALTER query
db.execSQL("ALTER ...");
}
db.setTransactionSuccessful();
} catch(Exception ex){
Log.e(this.getClass().getName(), ex.getMessage(), ex);
} finally {
// Ends transaction
// If there was an error, the database won't be altered
db.endTransaction();
}
}
}
そして、すべての中で最も簡単な部分: クエリを実行します。
String[] rows = new String[] {"_ID", "_name", "_email" };
Uri uri = Uri.parse("content://com.yourname.yourapp.Items/item/2";
// Alternatively you can also use getContentResolver().insert/update/query/delete methods
Cursor c = managedQuery(uri, rows, "someRow=1", null, null);
それが基本的にすべてであり、私が知る限り最もエレガントな方法です。