2011-12-30 88 views
1

我有两个表,我想使用列表活动在列表视图中显示两个部分。但只有其中一个表格正在显示。我有一个外键设置,但除了一个表格显示外没有其他设置。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); 
} 

我对此没有任何错误,但正如我所说,它只显示一个表。所有的帮助表示赞赏。

+0

请查看[其它问题] [1]提供帮助。 [1]:http://stackoverflow.com/questions/8731603/error-on-selectfrom-statement-android-sqlite-database – Christian 2012-01-04 18:00:32

回答

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

+0

什么会'propertyId'是什么? – Christian 2011-12-31 05:09:16

+0

property_id可以是基于您的要求的table_a或table_b的Unique_Id 或 property_id是您想要搜索数据的基础上的列 – 2011-12-31 05:15:13

+0

我不确定我是否正在执行此操作'private final String MY_QUERY =“SELECT * FROM wwTable mLat INNER JOIN wwTimeTable _id ON wwTable._id = wwTimeTable.mName WHERE wwTimeTable.mForeign =?“;''return ourdb.rawQuery(MY_QUERY,new String [] {String.valueOf(KEY_FOREIGN)});'什么是我在这里做错了吗? – Christian 2011-12-31 05:23:23

相关问题