A
ContentProvider
http://developer.android.com/guide/topics/providers/content-providers.html
第二个你需要的是
SQLiteOpenHelper
Cursor
db
变量为。它是SQLiteOpenHelper或者更好的我的扩展(在这里我只重写了onCreate、onUpgrade和构造函数)。见下文^^
ContentProvider将与数据库通信并执行插入、更新和删除操作。内容提供者还将允许代码的其他部分(即使是其他应用程序,如果您允许的话)访问存储在sqlite中的数据。
然后可以重写insert/delete/query/update函数并向其中添加功能,例如,根据意图的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;
}
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);
}
这样,您就可以决定是只返回或更新一个结果,还是全部查询。
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);
就我所知,这基本上是最优雅的方式。