【问题标题】:Android - How to retrieve data from multiple SQLite tables?Android - 如何从多个 SQLite 表中检索数据?
【发布时间】:2011-12-30 19:37:32
【问题描述】:

我有两个表格,我想使用列表 Activity 在列表视图中显示这两个表格的一部分。但只有一张表显示。我有一个外键集,但除了显示一个表之外什么都没有。

我的表格

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

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

【问题讨论】:

标签: android sqlite


【解决方案1】:

使用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=?";

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

【讨论】:

  • property_id 可以是 table_a 或 table_b 根据您的要求的 Unique_Id 或 property_id 是您要搜索数据的列
  • 我不确定我这样做是否正确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)});我在这里做错了什么?
  • 你能提供你的表结构吗? Table1(TableId, column1, column2) Table2(Table2Id, TableId (AS FK), column2, column3)
  • 是的,我已经在顶部的桌子下提供了这一点
  • 请查看我设置的其他问题以获取更多详细信息。 stackoverflow.com/questions/8731603/…
猜你喜欢
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
  • 2015-02-02
  • 2019-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-16
相关资源
最近更新 更多