是否有可能在Android中使用的ContentProvider和数据直接访问数据库? [英] Is it possible to use ContentProvider and direct data access to Database in Android?

查看:420
本文介绍了是否有可能在Android中使用的ContentProvider和数据直接访问数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有应用程序,使用的直接数据访问它的数据库。我没有什么要揭露它之外的其他应用程序。由于CommonsWare地方上说的,如果你没有揭露 - 不使用的ContentProvider。现在,我想添加小部件从一个表显示了几个字符串,在数据库中存储。我应该重构的直接进入 code到的ContentProvider 格局?有没有办法使用的小工具来直接访问数据库的方法吗?有没有办法同时拥有的ContentProvider并在一个应用程序直接访问数据库?


解决方案

  

现在,我想添加小部件从一个表显示了几个字符串,存储数据库。


我要假定的小部件你真的是应用程序部件。


  

我应该重构直接访问code将ContentProvider的模式?


您不必因为你正在编写一个应用小工具来做到这一点而已。


  

有没有使用来自小部件直接访问数据库的方式?


当然,使用您使用的是其他地方在您的应用程序相同的机制。


  

有没有办法兼得的ContentProvider并在一个应用程序直接访问数据库?


是的,但你必须要小心一点,以确保您使用的是相同的 SQLiteDatabase 对象在这两种情况下,这样你就不会遇到线程问题。

I have application that uses direct data access to it's Database. I have nothing to expose outside of it for other applications. As CommonsWare says somewhere on SO, "if you have nothing to expose - don't use ContentProvider". Now, I want to add widget to display a couple of strings from one of the tables, storing in Database. Should I refactor the direct access code to the ContentProvider pattern? Is there a way to use direct access to Database from widget? Is there a way to have both ContentProvider and direct access to database in one application?

解决方案

Now, I want to add widget to display a couple of strings from one of the tables, storing in Database.

I am going to assume that by "widget" you really mean "app widget".

Should I refactor the direct access code to the ContentProvider pattern?

You do not have to do this just because you are writing an app widget.

Is there a way to use direct access to Database from widget?

Sure, using the same mechanisms you are using anywhere else in your app.

Is there a way to have both ContentProvider and direct access to database in one application?

Yes, though you have to be a bit careful to make sure that you are using the same SQLiteDatabase object in both cases, so you do not run into threading issues.

这篇关于是否有可能在Android中使用的ContentProvider和数据直接访问数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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