Is it good practice to use ContentProvider to handle database operations?

白昼怎懂夜的黑 提交于 2019-12-12 23:15:58

问题


First let you know I am new in Android.

  1. Is it good practice to use ContentProvider to handle database table operations only for one application?

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!