Android ContentProvider 数据库查询多表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2787548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:23:26  来源:igfitidea点击:

Android ContentProvider database query multiple tables

androidsqliteandroid-contentprovider

提问by Antek Drzewiecki

I'm writing an RSS reader for Android. I've faced a certain difficulty which the problem I can't resolve since databases aren't my expertise.. So i figured out maybe one of you could help me out! I currently have 3 tables (Categories, links and feeds). My goal is too link a feed to multiple categories. Therefor I'm using a Link table. My databases is an Android ContentProvider (sqlite) and looks like the following:

我正在为 Android 编写一个 RSS 阅读器。我遇到了一些我无法解决的问题,因为数据库不是我的专长..所以我想也许你们中的一个可以帮助我!我目前有 3 个表(类别、链接和提要)。我的目标是将提要链接到多个类别。因此,我正在使用链接表。我的数据库是一个 Android ContentProvider (sqlite),如下所示:

| Categories |          |  Links  |          | Feeds |
|------------|          |---------|          |-------|
|  _ID       |          | Category|          | _ID   | 
|  Title     |          | Feed    |          | Title |
                                             | URL   |

I currently wrote the following code in my FeedListActivity to retrieve a list of links and their feeds.

我目前在我的 FeedListActivity 中编写了以下代码来检索链接及其提要的列表。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //gets the intent URI to retrieve the Feeds.
    Intent intent = getIntent();
    if (intent.getData() == null) {
        intent.setData(Feeds.CONTENT_URI);
    }
    // stores the Category id so it knows from what category it came from
    mCatId = intent.getIntExtra("catId", -1);   
    getListView().setOnCreateContextMenuListener(this);
    // gets all Links that have the category placed
    Cursor links = managedQuery(
            Links.CONTENT_URI, 
            NewsReadUtils.LINK_PROJECTION_STRING, 
            Links.CATEGORY+ " = "+ mCatId, null, null);
    String query = "";
    Cursor cursor = null;
    if(links.getCount()>0){
            // if there are links available try to get them and build a query.
        links.moveToFirst();
        do{
            if (links.isFirst()) {
                query = Feeds._ID+ " = "+links.getString(links.getColumnIndex(Links.FEED));                 
            }else{
                query += " OR " +Feeds._ID+ " = "+links.getString(links.getColumnIndex(Links.FEED));

            }               
        }while(links.moveToNext()); 
        cursor = managedQuery(getIntent().getData(), PROJECTION ,query, null,
                Feeds.DEFAULT_SORT_ORDER);
    }
    Cursor cursor = managedQuery(getIntent().getData(), PROJECTION ,Feeds._ID+ " = "+ mCatId, null,
            Feeds.DEFAULT_SORT_ORDER);
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor,
            new String[] { Feeds.TITLE }, new int[] { android.R.id.text1 });
    setListAdapter(adapter);       

}

Now my question:

现在我的问题:

I was wondering how I could optimize this database layout, code or query so I would get my entries in a more efficient way. Because i believe this link table or the query to retrieve links isn't needed! Or am i doing this the correct way?

我想知道如何优化此数据库布局、代码或查询,以便以更有效的方式获取我的条目。因为我相信不需要这个链接表或检索链接的查询!或者我这样做是正确的方法吗?

Thanks,

谢谢,

Antek

安泰克

回答by Juri

While CommonsWare's answer is right to some degree I find it doesn't fully answer the question.

虽然 CommonsWare 的回答在某种程度上是正确的,但我发现它并没有完全回答这个问题。

Normally ContentProviders are a way to share/expose the application's data and in fact this may often be the case. Still, there may arise the situation where one needs to join multiple tables using a ContentProvider.

通常 ContentProviders 是一种共享/公开应用程序数据的方式,实际上这可能经常是这种情况。不过,可能会出现需要使用 ContentProvider 连接多个表的情况。

The great thing about Android is that it is Open Source, so nothing prevents you from going to search in the Android apps for similar scenarios. This is what I usually do in such situations. The ContactsProvider.javais a good example. It's a bit complex but working through it may give some insights on how to use joins within ContentProviders. If you do not want to download the source you may found it on GitHub:

Android 的伟大之处在于它是开源的,所以没有什么能阻止您在 Android 应用程序中搜索类似的场景。在这种情况下,我通常会这样做。这ContactsProvider.java是一个很好的例子。它有点复杂,但通过它可能会提供一些关于如何在 ContentProviders 中使用连接的见解。如果您不想下载源代码,您可以在 GitHub 上找到它:

https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/ContactsProvider2.java

https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/ContactsProvider2.java

Good luck :)

祝你好运 :)