Android的ContentProvider的数据库查询多个表多个、数据库查询、Android、ContentProvider

2023-09-03 20:40:13 作者:哥丶看破繁华@

我在写一个RSS阅读器为Android。我已经遇到了一定的困难这问题我解决不了,因为数据库是不是我的专长。所以我想通了,也许你们中间有一个可以帮助我!我现在有3个表(分类,链接和饲料)。我的目标是进料过多链接到多个类别。为此我使用一个链接表。我的数据库是一个Android的ContentProvider(源码),看起来像下面这样:

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:

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

目前,我写了下面的code在我FeedListActivity检索链接和他们提要列表。

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

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);       

}

现在我的问题:

我想知道我怎么能优化这个数据库布局,code或查询,所以我会得到我的作品以更有效的方式。因为我相信这个链接表或查询检索链接是没有必要!还是我这样做的正确方法?

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?

谢谢

ANTEK

推荐答案

虽然CommonsWare的答案是正确的在一定程度上我发现它并没有完全回答这个问题。

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

通常ContentProviders是一个方式来分享/暴露应用程序的数据并在实际上这可能经常是这种情况。尽管如此,仍有可能出现,其中一个需要使用的ContentProvider加入多个表的情况。

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.

关于Android的伟大的事情是,它是开源的,所以没有什么prevents你打算在类似情况下的Andr​​oid应用程序进行搜索。这是我平时做在这种情况下。该 ContactsProvider.java 是一个很好的例子。这是一个有点复杂,但通过它的工作可能会如何使用ContentProviders中加入了一些见解。如果你不想下载源代码,你会发现它在GitHub上:

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.java is 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:

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

祝你好运:)

 
精彩推荐
图片推荐