问题描述
我的Android应用程序读取,并从几个不同的活动和服务写入本地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 ...)?
- 如何协调当地的Android数据库与服务器端的数据库中可用的结构通过REST接口?
是的,我知道我得到的常年哪来的Android的对象关系映射框架?题。现在,我主要是好奇,想听听你如何组织你的Android应用程序有什么是可以在标准的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语句的处理方式我ORMLite的实用程序类。大多数SQL是采取由DAO类保健。
- 解释类层次的在线文档的Android的部分。您可以实现一个
DatabaseHelper
这有助于创建一个更新数据库。你的活动扩展OrmLiteBaseActivity
(或服务或选项卡),它可以访问帮手和DAO的。 - 在ORMLite不与远程REST服务器合并提供了解决方案。
- 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 extendOrmLiteBaseActivity
(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的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!