SQLite的DB和ContentProvider的最佳实践 [英] Best practices for SQLite DB and ContentProvider

查看:416
本文介绍了SQLite的DB和ContentProvider的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序读取,并从几个不同的活动和服务写入本地SQLite数据库。 pretty的标准。但是我不是很满意我得为常量,我再在任何地方使用我访问数据库存储的所有数据库的详细信息的方式。我被告知包裹数据库中的ContentProvider。听起来不错。虽然我重构我的code,我想我会问:

My Android app is reading and writing to a local SQLite DB from a few different Activities and a Service. Pretty standard. But I'm not happy with the way I've got all the DB details stored as constants that I then use anywhere I access the DB. I've been advised to wrap the DB in a ContentProvider. Sounds good to me. While I'm refactoring my code, I figured I'd ask:

  • 什么是本地数据库的数据存储在Android中的最佳做法?
  • 在哪里,你怎么存储CREATE TABLE语句,列名,其它SQL?
  • 你不介意分享你实例化类和什么样的例子不胜枚举到每个(ContentProvider的,DatabaseProvider,DatabaseHelper ...)?
  • 如何协调当地的Andr​​oid数据库与服务器端的数据库中可用的结构通过REST接口?

是的,我知道我得到的常年哪来的Andr​​oid的对象关系映射框架?题。现在,我主要是好奇,想听听你如何组织你的Andr​​oid应用程序有什么是可以在标准的SDK。

Yeah, I realize I'm getting at the perennial "where's the Android object-relation-mapping framework?" question. For now, I'm mainly curious to hear how you structure your Android apps with what's available in the standard SDK.

和往常一样,感谢您的指点!

As always, thanks for the pointers!

推荐答案

我们已经调整 ORMLite 在Android现在一段时间,它是运作良好。 ORMLite支持Android的原生数据库调用,也支持通过JDBC等数据库。你注释你的类/字段和使用基础DAO类将持续到SQLite的。

We have been tuning ORMLite on Android for a while now and it is working well. ORMLite supports Android with native database calls and also supports other databases via JDBC. You annotate your classes/fields and use base DAO classes to persist to SQLite.

  • CREATE TABLE statements are handled my ORMLite's utility classes. Most SQL is taken care of by the DAO classes.
  • The Android section of the online documentation explains the class hierarchy. You implement a DatabaseHelper which helps create an update your database. Your activities extend OrmLiteBaseActivity (or service or tab) which gives access to helper and DAOs.
  • ORMLite does not provide a solution for merging with remote REST servers.

希望这有点帮助。

这篇关于SQLite的DB和ContentProvider的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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