iTurki
iTurki

Reputation: 16398

Strange Problem with Cursor - Android

This is a method In my DataBase Class:

public Cursor fetchFavTitles() {
    return myDataBase.rawQuery("SELECT rowid as _id, title
        FROM table1 JOIN table2 JOIN table3 JOIN table4 JOIN table5 JOIN table6
        WHERE fav = TRUE", null);
}

My SQLite database has 6 tables:

  1. table1 => rowid, title, content, fav
  2. table2 => rowid, title, content, fav
  3. table3 => rowid, title, content, fav
  4. table4 => rowid, title, content, fav
  5. table5 => rowid, title, content, fav
  6. table6 => rowid, title, fav

In my activity, I wrote this:

Cursor cursor = myDbHelper.fetchFavTitles();

and the application forces the close!

Any idea where I'm mistaken ?

UPDATE

This is a snapshot of the LogCat, I couldn't understand it, I filtered the output with android.database: enter image description here

What I am trying to do is getting the title (type: TEXT) that have a fav (type: BOOL) with value TRUE From all the tables and display them in one ListView (using SimpleCursorAdapter).

Upvotes: 0

Views: 356

Answers (1)

E.Z. Hart
E.Z. Hart

Reputation: 5747

Without understanding exactly what you're going for, I'm guessing you need to change your query to:

SELECT rowid as _id, title
        FROM table1 
        WHERE fav = TRUE
UNION ALL
SELECT rowid as _id, title
        FROM table2
        WHERE fav = TRUE
UNION ALL
SELECT rowid as _id, title
        FROM table3
        WHERE fav = TRUE
UNION ALL   
SELECT rowid as _id, title
        FROM table4 
        WHERE fav = TRUE
UNION ALL       
SELECT rowid as _id, title
        FROM table5 
        WHERE fav = TRUE
UNION ALL       
SELECT rowid as _id, title
        FROM table6 
        WHERE fav = TRUE

This will take all the results where 'fav = TRUE' from each of the tables and put them all into one result set. If you don't want duplicates, you can change 'UNION ALL' to 'UNION'. Right now your query is failing because 'SELECT rowid as _id, title' doesn't know which of your tables to pull the 'title' field from.

Upvotes: 1

Related Questions