Android - 如何从多个SQLite表中检索数据?

时间:2011-12-30 19:37:33

标签: android sqlite

我有两个表,我希望使用列表活动在列表视图中显示两个表的一部分。但只有一个表格正在显示。我有一个外键设置,但除了一个表显示之外什么都没有。

我的表格

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

我如何查询信息

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


}

查看信息的位置

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

我没有得到任何错误,但就像我说它只显示一个表。感谢所有帮助。

1 个答案:

答案 0 :(得分:2)

使用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)});