如何组织数据库访问code在Android项目? [英] How to organize database access code in Android project?

查看:112
本文介绍了如何组织数据库访问code在Android项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了我的主要SQLite表ContentProvider的,pretty多少以下的记事本例如,从SDK (虽然我不知道是否我将永远不会暴露我的数据到其他应用程序)。但是,我需要创建很多其他的,不平凡的查询上和其他表和视图。一个很好的例子是查询提取基础的一些统计数据,平均值,总数等。

I have created a ContentProvider for my main Sqlite table, pretty much following NotePad example from SDK (although I am not sure whether I will ever be exposing my data to other apps). However, I need to create lots of other, non-trivial queries on that and other tables and views. A good example would be queries to extract some statistics from the base data, averages, totals etc.

那么,什么是这个code最好的地方在Android项目?应该如何相关,并连接到由提供商露出的基于URI的数据存取?任何好的例子在那里?

So what's the best place for this code in an Android project? How it should be related and connected to the Uri-based data access exposed by a Provider? Any good examples out there?

推荐答案

从视图maintainabillty的一点,我觉得供应商模式是抽象的数据访问code最彻底的方法。而从经验上有一个大的应用程序工作,最终应用将增长到一些数据必须通过提供者模型被曝光(例如引入服务应用程序)的地步。这就是说暴露你的数据很多不同的意见可以是很多工作的提供者模型。

From the point of view of maintainabillty I think that the provider model is the cleanest way to abstract the data access code. And from experience having worked on a large application, eventually the application will grow to the point where some data must be exposed via the provider model (e.g. introducing services to the application). That said exposing many different views of your data can be a lot of work in the provider model.

如果你去你如何暴露目标URL数据我会仔细考虑的路线,通常一些你说的可以使用子目录重数据的presenting不同意见的管理复杂性(类似于REST方法)。

If you were to go the route I would think carefully how you exposed the data through URLs, and usually some of the complexity you talk of can be managed with use of sub-directories representing different views of the data (similar to a REST approach).

如果你希望避免的提供者模型,然后实施DA类是相当简单的。通常,SQLiteOpenHelper是creatd作为一个内部类的DA类(开放助手也提供基本的版本支持)和这个类的一个实例用于创建在DA功能的数据库连接。
有在Android源代码code的例子很多。看任何供应商实现的,用<一个href=\"http://android.git.kernel.org/?p=platform/packages/providers/MediaProvider.git;a=blob;f=src/com/android/providers/media/MediaProvider.java;hb=HEAD\"相对=nofollow> MediaProvider.java 中可能是最相关的,因为它使用了非常复杂的搜索提供商。例如从源头code不是内容提供商看到<一个href=\"http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/com/android/internal/net/DbSSLSessionCache.java;hb=HEAD\"相对=nofollow> DbSSLSessionCache.java

If you wish to avoid the provider model, then implementing a DA class is fairly straight forward. Typically a SQLiteOpenHelper is creatd as an inner class to the DA class (the open helper also providers basic versioning support) and an instance of this class is used to create database connections in the DA functions. There are many examples in the Android Source code. Look at any of the provider implementations, with MediaProvider.java probably being the most relevant as it uses quite complex searches for a provider. For example from the source code that is not a content provider see DbSSLSessionCache.java

这篇关于如何组织数据库访问code在Android项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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