Christian
Christian

Reputation: 942

Android - How to retrieve data from multiple SQLite tables?

I have two tables and I want to show parts of both in a list view using a list Activity. But only one of the tables is showing. I have a foreign key set but nothing except the one table is showing.

My Tables

db.execSQL("CREATE TABLE " + WW_TABLE + 
                " (" + KEY_ROWIDLL + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_LAT + " TEXT NOT NULL, " + 
                KEY_LON + " TEXT NOT NULL);");
        db.execSQL("CREATE TABLE " + WW_TIMETABLE + 
                " (" + KEY_ROWIDTIME + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_HOUR + " TEXT NOT NULL, " +
                KEY_FOREIGN + " INTEGER," +
                " FOREIGN KEY ("+KEY_FOREIGN+") REFERENCES "+WW_TABLE+" ("+KEY_ROWIDLL+") ON DELETE CASCADE);");

How I query the information

public Cursor fetchTime() {
    // TODO Auto-generated method stub
    return ourdb.query(WW_TIMETABLE, new String[] {KEY_ROWIDTIME, KEY_HOUR, KEY_FOREIGN}, null, null, null, null, null);


}

Where I View the Information

private void fillData() {
     // Get all of the rows from the database and create the item list

    mTimeNotesCursor = mDbHelper.fetchTime();
    startManagingCursor(mTimeNotesCursor);
   // startManagingCursor(mNotesCursor);

    // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{WWDatabase.KEY_HOUR,WWDatabase.KEY_FOREIGN};

    //String[] fromTime = new String[]{WWDatabase.KEY_HOUR};


    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.textView2, R.id.textView3};

   //int[] toTime = new int[]{R.id.textView4};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = 
        new SimpleCursorAdapter(this, R.layout.time_text_row, mTimeNotesCursor, from, to);

    setListAdapter(notes);
   // SimpleCursorAdapter notesTime = 

       //new SimpleCursorAdapter(this, R.layout.time_text_row, mTimeNotesCursor, fromTime, toTime);
        //setListAdapter(notesTime);
}

I do not get any errors with this, But like I said it only shows the one table. All help is appreciated.

Upvotes: 1

Views: 5257

Answers (1)

Yaqub Ahmad
Yaqub Ahmad

Reputation: 27659

Use the rawQuery:

private final String MY_QUERY = "SELECT * FROM table_a a INNER JOIN table_b b ON a.id=b.other_id WHERE b.property_id=?";

OR

private final String MY_QUERY = "SELECT * FROM table_a a INNER JOIN table_b b ON a.id=b.other_id WHERE a.property_id=?";

db.rawQuery(MY_QUERY, new String[]{String.valueOf(propertyId)});

Upvotes: 2

Related Questions