热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SQLiteDB和ContentProvider的最佳实践-BestpracticesforSQLiteDBandContentProvider

MyAndroidappisreadingandwritingtoalocalSQLiteDBfromafewdifferentActivitiesandaSer

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应用程序从一些不同的活动和服务中读写到本地的SQLite DB。非常标准的。但是我不喜欢把所有的DB细节都存储为常量,然后在访问DB的任何地方使用。有人建议我将DB包在一个内容提供程序中。听起来不错。当我重构代码的时候,我想我会问:

  • What are your best practices for local DB data storage in Android?
  • Android中本地DB数据存储的最佳实践是什么?
  • Where and how do you store "CREATE TABLE" statements, column names, other SQL?
  • 在何处以及如何存储“创建表”语句、列名和其他SQL?
  • Would you mind sharing a list of the classes you instantiate and what goes into each (ContentProvider, DatabaseProvider, DatabaseHelper...)?
  • 您是否介意共享实例化的类的列表以及每个类的内容(内容提供者、数据库提供者、数据库助手…)?
  • How do you coordinate the structure of your local Android DB with a server-side DB available through a REST interface?
  • 如何协调本地Android DB的结构与通过REST接口提供的服务器端DB ?

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.

是的,我意识到我一直在问“Android对象-关系-映射框架在哪里?”现在,我主要想知道如何用标准SDK中可用的方式来构建Android应用程序。

As always, thanks for the pointers!

一如既往,谢谢你的指点!

5 个解决方案

#1


16  

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.

我们已经在Android上对ORMLite进行了一段时间的调试,并且运行良好。ORMLite通过本地数据库调用支持Android,并通过JDBC支持其他数据库。您可以注释您的类/字段,并使用基础DAO类来持久化到SQLite。

  • CREATE TABLE statements are handled my ORMLite's utility classes. Most SQL is taken care of by the DAO classes.
  • CREATE TABLE语句是处理我的ORMLite的实用程序类的。大多数SQL都由DAO类处理。
  • 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.
  • 在线文档的Android部分解释了类层次结构。实现一个DatabaseHelper,它可以帮助创建更新数据库。您的活动扩展了OrmLiteBaseActivity(或服务或选项卡),该活动允许访问helper和DAOs。
  • ORMLite does not provide a solution for merging with remote REST servers.
  • ORMLite没有提供与远程REST服务器合并的解决方案。

Hope this is somewhat helpful.

希望这能有所帮助。

#2


1  

For now, I'm mainly curious to hear how you structure your Android apps with what's available in the standard SDK.

现在,我主要想知道如何用标准SDK中可用的方式来构建Android应用程序。

I'm not a real fan of SQL and the way that it's handled in android, so I use the object database NeoDatis. It basically just lets you store / retrieve Java objects into a flat file stored on the device very easily. db40 is also another alternative Object Database that will work on android.

我不太喜欢SQL,也不喜欢android中处理SQL的方式,所以我使用对象数据库NeoDatis。它基本上只允许您将Java对象存储/检索到存储在设备上的平面文件中。db40也是android上的另一种对象数据库。

Haven't had any issues using this approach, you may want to note that including the NeoDatis library will increase your APK size by ~700kb.

使用这种方法没有任何问题,您可能需要注意,包括NeoDatis库将使APK大小增加大约700kb。

#3


0  

I don't know that I have an answer, other than I don't really like how this is handled, I find it very messy as well. I usually follow the pattern given in the notepad example that comes w/ the SDK.

我不知道我有答案,除了我不喜欢这个怎么处理,我觉得它也很乱。我通常遵循来自w/ SDK的notepad示例中的模式。

Due to this I am working on my own mini ORM framework, using annotation and managing all of this. So far things are working ok, but I haven't worked everything out yet.

因此,我正在开发我自己的迷你ORM框架,使用注释并管理所有这些。到目前为止一切正常,但我还没有把所有的事情都解决。

#4


0  

You could also have a look at Androrm. It is an open source orm tool designed espacially for android. It should help you with all database related stuff.

你也可以看看仙女座。它是一个开源的orm工具,专门为android设计。它将帮助您处理所有与数据库相关的内容。

#5


0  

Just to complete the list more and more ... Another ORM is the ORM solution provided with the BARACUS Framework. It is not inteded to build enterprise sized databases, its more for storing a couple of entities in the database and make it accessible by the app. There is no codegeneration approach inside of it; you simply write your entity pojo, a rowmapper and your table def. Therefore you can make use of DAOs, Dependency Injection, IOC style lifecycle support and much more.

只是为了越来越多地完成这个列表……另一个ORM是由BARACUS框架提供的ORM解决方案。它不是为了构建企业规模的数据库,而是为了在数据库中存储两个实体,并使其可以通过应用程序访问。您只需编写实体pojo、rowmapper和表def,因此可以使用dao、依赖注入、IOC样式的生命周期支持等等。

ORM Features so far :

ORM特性:

  • Lazy Loading (Collections + Lazy References)
  • 延迟加载(集合+延迟引用)
  • CRUD operations for free
  • CRUD操作免费
  • Query by example
  • 实例查询
  • a couple of aspects (autotimestamp etc)
  • 几个方面(自动时间戳等)
  • Persistence layer lifecycle support across multiple application releases
  • 跨多个应用程序版本的持久性层生命周期支持

For the more sophisticated database stuff (using the ORM is a little bit hand work like in good old spring rowmapper time) I am currently thinking about adding ormlite integration.

对于更复杂的数据库内容(使用ORM是一种手工操作,就像以前的spring rowmapper时代一样),我目前正在考虑添加ormlite集成。

For more code details just check the tutorial application on github

有关更多的代码细节,请查看github上的教程应用程序。


推荐阅读
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 标题: ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • intellij idea的安装与使用(保姆级教程)
    intellijidea的安装与使用(保姆级教程)IntelliJ在业界被公认为最好的java开发工具,尤其在智能代码助手、代码自动提示、重构、JavaEE支持、各类版本工具(gi ... [详细]
  • 本文介绍了在使用Laravel和sqlsrv连接到SQL Server 2016时,如何在插入查询中使用输出子句,并返回所需的值。同时讨论了使用CreatedOn字段返回最近创建的行的解决方法以及使用Eloquent模型创建后,值正确插入数据库但没有返回uniqueidentifier字段的问题。最后给出了一个示例代码。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 闭包一直是Java社区中争论不断的话题,很多语言都支持闭包这个语言特性,闭包定义了一个依赖于外部环境的自由变量的函数,这个函数能够访问外部环境的变量。本文以JavaScript的一个闭包为例,介绍了闭包的定义和特性。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • MySQL语句大全:创建、授权、查询、修改等【MySQL】的使用方法详解
    本文详细介绍了MySQL语句的使用方法,包括创建用户、授权、查询、修改等操作。通过连接MySQL数据库,可以使用命令创建用户,并指定该用户在哪个主机上可以登录。同时,还可以设置用户的登录密码。通过本文,您可以全面了解MySQL语句的使用方法。 ... [详细]
  • 本文介绍了一个免费的asp.net控件,该控件具备数据显示、录入、更新、删除等功能。它比datagrid更易用、更实用,同时具备多种功能,例如属性设置、数据排序、字段类型格式化显示、密码字段支持、图像字段上传和生成缩略图等。此外,它还提供了数据验证、日期选择器、数字选择器等功能,以及防止注入攻击、非本页提交和自动分页技术等安全性和性能优化功能。最后,该控件还支持字段值合计和数据导出功能。总之,该控件功能强大且免费,适用于asp.net开发。 ... [详细]
author-avatar
路很长别太狂_297
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有