如何使用的ContentProvider的insert()方法,通过CursorLoader正确插入值到SQLite数据库? [英] How to properly insert values into the SQLite database using ContentProvider's insert() method through a CursorLoader?

本文介绍了如何使用的ContentProvider的insert()方法,通过CursorLoader正确插入值到SQLite数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在阅​​读文档,但我还是不太清楚。它说,使用 getContentResolver(),但真的没有使用CursorLoader。那么,有没有办法通过 CursorLoader 办呢?我知道如何与查询做()。是步骤很相似?即使只是正好说明一个链接,这将是有益的。

I was reading the doc, but I am still not too sure. Its says to use getContentResolver(), but then that really isn't using CursorLoader. So is there a way to do it through CursorLoader? I know how to do it with query(). Are the steps very similar? Even just a link that explains exactly this would be helpful.

请注意,不要联系我到谷歌文档,因为他们没有使用<$ C $是有史以来使用从ContentProvider的的插入()方法的一个例子C> CursorLoader 。

Please note, do not link me to the Google doc as they do not have an example that ever uses the insert() method from ContentProvider using a CursorLoader.

在此先感谢!

编辑:我也许应该提及我感到困惑与此的原因是因为调用一个新的 CursorLoader 自动调用 ContentProviders 查询()方法。但我怎么可以做同样的插入?

I should probably mention the reason I am confused with this is because calling a new CursorLoader automatically calls ContentProviders query() method. But how can I do the same for insert?

推荐答案

看看我的<一个href="http://www.androiddesignpatterns.com/2012/06/content-resolvers-and-content-providers.html">blog帖子关于这个问题的:

Check out my blog post on the subject:

<一个href="http://www.androiddesignpatterns.com/2012/06/content-resolvers-and-content-providers.html">

Content解析器和内容提供商

插入是一个完全不同的概念......它绝对的没有的做的 CursorLoader 。当与 LoaderManager CursorLoader 自动查询数据库和更新自己当 ContentObserver 通知数据存储的变化。它无关,与将数据插入你的数据库的实际过程。

Insertion is a totally different concept... it has absolutely nothing to do with the CursorLoader. When coupled with the LoaderManager, the CursorLoader automatically queries your database and updates itself when the ContentObserver is notified of a datastore change. It has nothing to do with the actual process of inserting data into your database.

当你插入(或查询或更新或删除)数据到通过内容提供商的数据库,你不直接与供应商沟通。相反,您可以使用 ContentResolver的对象提供者(请注意, ContentResolver的是一个私有的实例变量来传达你的应用程序的全球上下文)。更具体地说,步骤进行的顺序是:

When you insert (or query or update or delete) data into your database via the content provider, you don't communicate with the provider directly. Instead, you use the ContentResolver object to communicate with the provider (note that the ContentResolver is a private instance variable in your application's global Context) . More specifically, the sequence of steps performed is:

  1. 您拨打 getContentResolver()插入(URI,ContentValues​​);

ContentResolver的对象确定URI的权威。

The ContentResolver object determines the authority of the Uri.

ContentResolver的转发请求与机关登记的内容提供商(这就是为什么你需要指定的权限在 AndroidManifest.xml中)。

The ContentResolver relays the request to the content provider registered with the authority (this is why you need to specify the authority in the AndroidManifest.xml).

内容提供商接收请求并执行指定的操作(在这种情况下,插入)。如何以及在何处被插入的数据取决于你如何实施插入办法(的ContentProvider 是需要一个抽象类用户实施插入查询删除更新的getType )。

The content provider receives the request and performs the specified operation (in this case insert). How and where the data is inserted depends on how you implemented the insert method (ContentProvider is an abstract class that requires the user to implement insert, query, delete, update, and getType).

希望您能环绕,至少有一点你的头。之所以有涉及如此多的步骤,是由于Android(1)允许应用程序有多个内容提供商,和(2)需要确保应用程序可以与其他第三方应用程序安全地共享数据。 (这是不是因为它想迷惑你,我保证)。

Hopefully you were able to wrap your head around that at least a little. The reason why there are so many steps involved is because Android (1) allows applications to have more than one content provider, and (2) needs to ensure that apps can securely share data with other third-party apps. (It wasn't because it wanted to confuse you, I promise).

现在,你(希望)有一个更好地了解如何在 ContentResolver的能够传递这些请求的内容提供商,插入数据是相当简单:

Now that you (hopefully) have a better idea of how the ContentResolver is able to relay these requests to the content provider, inserting the data is fairly straight forward:

  1. 首先,决定你想要有相匹配的内容提供商其中URI。这取决于你如何决定与 UriMatcher 匹配您的URI。你已经重新$ P $每个URI psents将数据插入到你的内部数据库(例如,如果你的应用程序有两个表,您将可能有两个URI,每个表)。

  1. First, decide which uri you want to have matched by your content provider. This depends on how you decided to match your uris with the UriMatcher. Each uri you have represents a different means of inserting data into your internal database (i.e. if your app has two tables, you will probably have two uris, one for each table).

创建一个新的 ContentValues​​ 对象,并用它来包装你想发送给内容提供商的数据。该 ContentValues​​ 对象映射列名的数据值。在下面的例子中,列名是列下被插入COLUMN_1和值是_1:

Create a new ContentValues object and use it to package the data you wish to send to the content provider. The ContentValues object maps column names to data values. In the below example, the column name is "column_1" and the value being inserted under that column is "value_1":

ContentValues values = new ContentValues();
values.putString("column_1", "value_1");

  • 一旦收到,内容提供商将(在你的情况下),通过对象的 SQLiteDatabase (通过 SQLiteDatabase.insert(字符串表,字符串nullColumnHack,ContentValues​​值)法)。不同于的ContentProvider ,此方法为您实现...的 SQLiteDatabase 知道如何处理的对象,将插入一行到数据库中,返回插入的行的行号,或 1 如果插入失败。

  • Once received, the content provider will (in your case) pass the values object to your SQLiteDatabase (via the SQLiteDatabase.insert(String table, String nullColumnHack, ContentValues values) method). Unlike the ContentProvider, this method is implemented for you... the SQLiteDatabase knows how to handle the values object and will insert the row into the database, returning the row id of the inserted row, or -1 if the insertion failed.

    ...这是你如何将数据插入到数据库。

    ... and that's how you insert data into your database.

    使用 getContentResolver()插入(URI,ContentValues​​);

    这篇关于如何使用的ContentProvider的insert()方法,通过CursorLoader正确插入值到SQLite数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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