它是很好的做法,使用的ContentProvider来处理数据库的操作? [英] Is it good practice to use ContentProvider to handle database operations?

查看:273
本文介绍了它是很好的做法,使用的ContentProvider来处理数据库的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先让你知道我在Android的是新。


  1. 它是很好的做法,使用的ContentProvider处理数据库表操作仅仅为一个应用程序?


  2. 试图创建多个类来处理数据库表操作。创建了一个数据库帮手如下:

    公共类WSDatabaseHelper扩展SQLiteOpenHelper {

     私有静态最后弦乐DATABASE_NAME =wsemp;
        私有静态最终诠释DATABASE_VERSION = 5;    公共WSDatabaseHelper(上下文的背景下){
            超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
        }    @覆盖
        公共无效的onCreate(SQLiteDatabase数据库){
            ItemTable.onCreate(数据库);
            CustomerTable.onCreate(数据库);
        }    @覆盖
    公共无效onUpgrade(SQLiteDatabase数据库,诠释oldVersion,诠释静态网页){
            ItemTable.onUpgrade(数据库,oldVersion,NEWVERSION);
            CustomerTable.onUpgrade(数据库,oldVersion,NEWVERSION);
        }
    }


创建一个类来处理数据库表操作:

 公共类CustomerBean {
    私人WSDatabaseHelper数据库;    @覆盖
    公共布尔的onCreate(){
        数据库=新WSDatabaseHelper(的getContext());
        返回false;
    }公共布尔insertObject(valObj){
    SQLiteDatabase分贝= database.getWritableDatabase();
    db.insert(CustomerTable.TABLE_CUST_ACCOUNT_INDEX,空,价值);
    }
}

但现在我不知道我怎样才能从我的活动或会议文件调用此函数insertObject。我试图通过CustomerBean.isnertObject(OBJ),但它要求的方法改为静态的。


解决方案

  

它是很好的做法,使用的ContentProvider处理数据库表操作仅仅为一个应用程序?


如果你的数据是独享您的应用程序和其他应用程序无法使用它,我看不出有任何理由使用ContentProviders。 ContentProvider的用作共享应用程序的数据到其他应用程序的接口。如果您的数据可以共享或其他应用程序依赖于它,那么你必须使用的ContentProvider。

您也可以创建权限设置为您的内容提供商在提供以限制某些操作。

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.

解决方案

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.

这篇关于它是很好的做法,使用的ContentProvider来处理数据库的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆