title: Android ContentProvider database query multiple tables
body: <p>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:</p>
  
  <pre><code>| Categories |          |  Links  |          | Feeds |
  |------------|          |---------|          |-------|
  |  _ID       |          | Category|          | _ID   | 
  |  Title     |          | Feed    |          | Title |
                                               | URL   |
  </code></pre>
  
  <p>I currently wrote the following code in my FeedListActivity to retrieve a list of links and their feeds.</p>
  
  <pre><code>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()&gt;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></pre>
  
  <p>Now my question:</p>
  
  <p>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?</p>
  
  <p>Thanks,</p>
  
  <p>Antek</p>