问题
First let you know I am new in Android.
Is it good practice to use ContentProvider to handle database table operations only for one application?
Trying to create multiple classes to handle database table operations. Created a database helper as follow:
public class WSDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "wsemp"; private static final int DATABASE_VERSION = 5; public WSDatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase database) { ItemTable.onCreate(database); CustomerTable.onCreate(database); } @Override public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { ItemTable.onUpgrade(database, oldVersion, newVersion); CustomerTable.onUpgrade(database, oldVersion, newVersion); } }
Created a class to handle database table operation:
public class CustomerBean {
private WSDatabaseHelper database;
@Override
public boolean onCreate() {
database = new WSDatabaseHelper(getContext());
return false;
}
public boolean insertObject(valObj) {
SQLiteDatabase db = database.getWritableDatabase();
db.insert(CustomerTable.TABLE_CUST_ACCOUNT_INDEX, null, values);
}
}
But now I am not sure how I can call this insertObject function from my activity or session file. I tried by CustomerBean.isnertObject(obj) but it's asking to change the method to static.
回答1:
Is it good practice to use ContentProvider to handle database table operations only for one application?
If your data is exclusive only for your application and other application cannot use it I don't see any reason to use ContentProviders. ContentProvider is used as an interface for sharing your application's data to other application. If your data can be shared or other application is dependent on it then you have to use ContentProvider.
Also you can create set of permissions to your content providers to restrict some operations in the provider.
来源:https://stackoverflow.com/questions/15004880/is-it-good-practice-to-use-contentprovider-to-handle-database-operations