Android的数据库访问设计方法 [英] Android Database Access Design Approach

查看:183
本文介绍了Android的数据库访问设计方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我身边有数据访问一般的Andr​​oid的设计问题。我有一些在我的应用程序需要访问一个SQLite数据库活​​动。为了包所有的数据访问逻辑,在一个地方我创建了一个DatbaseHandler类,采取所有数据访问逻辑的照顾。这个类需要建立where子句,调用数据库和询问产生的游标检索查询结果,并将其返回给调用者的照顾。这个类的用途是包住所有数据访问code在同一个地方,以便它可以容易地管理和维护,而不是具有分散在所有活动数据访问逻辑。每个需要访问数据库活动创建该数据库处理器类的一个实例,并将它android.content.Context的参考。然后,数据库处理器类使用这个上下文对象调用一个基本内容提供商如下context_i.getContentResolver()查询(...)。

I have a general Android design question around data access. I have a number of activities in my application that need to access a SQLite database. In order to wrap up all data access logic in one place I have created a DatbaseHandler class that takes care of all data access logic. This class takes care of building up where clauses, calling the database and interrogating the resulting cursor to retrieve the query results and return them to the caller. The purpose of this class is to wrap all data access code in one place so that it can be easily managed and maintained as opposed to having data access logic scattered across all activities. Each activity that needs access to the database creates an instance of this DatabaseHandler class and passes it a reference of android.content.Context. The DatabaseHandler class then uses this Context object to call an underlying content provider as follows context_i.getContentResolver().query(...).

我的数据访问逻辑(光标处理逻辑是具体的)是不是在活动,所以我不能管理光标生命周期,因此有可能是内存泄漏。

My data access logic (cursor handling logic to be specific) is not in the activity and so I cannot manage the cursors life cycle, therefore there is likely to be memory leaks.

我的问题如下 -


  1. 如何(如果它甚至有可能)从外活动管理的游标的生命周期?

  2. 应在每次活动甚至可以创建这个数据处理程序类的一个实例,并通过上下文的实例呢?也许我的设计方法是错误的,我应该公开这些数据访问功能作为采取调用活动作为参数的一个实例的静态方法。这样,我可以执行管理查询,让活动采取管理光标生命周期的照顾?

我真的想获得最好的方法的理解。任何意见将大大AP preciated

I'd really like to get an understanding of the best approach. Any advice would be greatly appreciated

推荐答案

的标准方法:结果
一般来说,如果你有自己写的ContentProvider的,你在你的manifest.xml文件正确注册它,你可以做(​​例如)

The standard approach:
Normally if you have a ContentProvider written by yourself and you registered it correctly in your manifest.xml file, you can just do (for instance)

@Override
public void onCreate(Bundle savedInstanceState){
   ...

   if (getIntent().getData() == null) {
       getIntent().setData(MyMetaData.CONTENT_URI);
   }

   Cursor cursor = managedQuery(getIntent().getData(), null, null, null, null);

   //create an appropriate adapter and bind it to the UI
   ...
}

这会自动调用ContentProvider的是能够处理给定内容的URI,给你注册在manifest.xml文件像

this will automatically call the ContentProvider that is able to handle the given content uri, given you registered it in the manifest.xml file like

<provider android:name="MyContentProvider" android:authorities="com.mycompany.contentprovider.MyContentProvider" />

我总是建议人们采取一看记事本的例子对于学习ContentProviders应该如何实现

I always suggest people to take a look at the Notepad example for learning how ContentProviders should be implemented.

替代:结果
一般来说,如果你需要只是你的活动范围内访问您的数据我会坚持的标准做法使用ContentProviders,其中顺便说一句。可能使它成为最灵活的解决方案。结果
如果您的解决方案的需求的从非活动课,你没有managedQuerymetods还访问数据,那么你可以执行某种DAO(数据访问对象)班的自己。一个例子可以是

Alternatives:
Generally speaking, if you need to access your data just within your activities I would stick to the "Standard approach" using ContentProviders, which btw. probably makes it the most flexible solution.
If your solution needs to access the data also from non-Activity classes where you don't have the "managedQuery" metods, then you could implement some kind of DAO (Data Access Object) classes yourself. An example could be

public class MyDataDao implements IMyDataDao {
   private ContentResolver contentResolver;

   public MyDataDao(ContentResolver contentResolver){
      this.contentResolver = contentResolver;
   }


   @Override
   public MyDataObject readMyDataObjectById(long id){
      MyDataObject result = null;

      Cursor myDataObjectCursor = contentResolver.query(...);
      if(myDataObjectCursor != null && myDataObjectCursor.moveToFirst()){
         result = new MyDataObject();
         result.setTitle(myDataObjectCursor.get..);
         ...
      }
      myDataObjectCursor.close();

      return result;
   }
}

这可以正常工作。然后调用你的DAO

That may work as well. You then call your DAO

IMyDataDao dao = new MyDataDao(context.getContentResolver());
MyDataObject anObj = dao.readMyDataObjectById(10);
...

。希望你指出了正确的方向:)

Hope that pointed you in the right direction :)

这篇关于Android的数据库访问设计方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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