2012-07-05 96 views
3

我正在創建一個應用程序作爲學習工具,並且在加入查詢時遇到困難。Android SQLite加入查詢

我有一個數據庫有兩個表 - 馬和封面 - 聲明如下;

private static final String HORSES_CREATE = "create table horses (_id integer primary key autoincrement, " 
     + "name text not null, type integer not null, birthDate text not null, vaccineDate text not null, " 
     + "inFoal integer not null, notes text not null);"; 

「類型」字段是指種馬,母馬,駒等,並從紡絲器(從XML字符串數組填充)來選擇。

private static final String COVERS_CREATE = "create table covers (_id integer primary key autoincrement, " 
     + "stallionName integer not null, mareName integer not null, firstCoverDate text not null, lastCoverDate text not null, " 
     + "scan14Date text not null, scan28Date text not null, foalingDate text not null, inFoal integer not null, notes text not null);"; 

stallionName實際上是從馬錶中存儲爲馬的_id字段。它是從一個微調器中選擇的,只顯示馬錶中被定義爲「馬」的馬的類型。 (這同樣適用於Mare)。

我有一個'DatabaseHelper'類來創建和升級表,每個表都有自己的適配器類'horsesDbAdapter'和'coversDbAdapter',它包含添加,編輯和刪除條目以及相關查詢的方法。 (fetchAllHorses(),fetchHorse(長ROWID))

如:

public Cursor fetchAllHorses() { 

    return mDb.query(DATABASE_TABLE, 
      new String[] { KEY_ROWID, KEY_NAME, KEY_TYPE, KEY_BIRTHDATE, 
        KEY_VACCINEDATE, KEY_INFOAL, KEY_NOTES }, null, null, 
      null, null, null); 
} 

(這是所有改編自Android的記事本爲例)

我有一個列表視圖顯示的封面表的內容(只顯示了stallionName和mareName)。但是,由於這些字段只包含對馬錶的唯一引用,所有顯示的內容都是相當無用的_id字段。

我的問題是;我怎樣才能得到相關的名字馬在listView中顯示?我讀過關於連接查詢等,但當我嘗試實現它們時會丟失。我假設我必須加入horses._id和covers.stallionName(然後爲MareName創建一個幾乎相同的文件),但我找不到如何執行此操作的具體示例。

請讓我知道是否需要任何額外的信息/代碼。

任何幫助將不勝感激,謝謝你提前。

編輯: 我已經在馬錶中做了stallionName和mareName外鍵引用(_id),但我仍然不確定如何以及在哪裏實現連接查詢;它應該在馬dbAdapter,coversDbAdapter或coversList類? (coversList是創建和填充listView的類) 覆蓋表聲明現在讀取;

private static final String COVERS_CREATE = "create table covers (_id integer primary key autoincrement, " 
     + "stallionName integer not null, mareName integer not null, firstCoverDate text not null, lastCoverDate text not null, " 
     + "scan14Date text not null, scan28Date text not null, foalingDate text not null, inFoal integer not null, notes text not null," + 
     "FOREIGN KEY (stallionName) REFERENCES horses (_id), FOREIGN KEY (mareName) REFERENCES horses (_id));"; 
+0

你應該建立在佔地表外鍵引用馬錶的相關列。 – Sajmon 2012-07-05 18:17:47

+0

@deceiver Thankyou的輸入。我已經將這添加到封面表聲明中,但我仍然不確定如何實際實現連接並讓它填充listView – Bruce0 2012-07-05 19:00:16

回答

2

我設法讓它工作。對於任何可能有類似問題的人,我會試着詳細說明我所做的事情。 由於@deceiver聲明我應該使用stallionName和mareName外鍵來引用馬匹(請參閱編輯)。

在coversList類(實現listView的類)我只需要獲取數據庫的一個實例,並使用rawQuery直接實現SQL代碼(這可能可以使用查詢,但我是不知道如何) 添加的代碼如下;

private Cursor coversCursor; 
private void fillData() { 

    db = (new DatabaseHelper(this).getReadableDatabase()); 
    coversCursor = db.rawQuery("SELECT horses1.name AS stallionNameText, covers.*, horses2.name AS mareNameText FROM horses horses1 JOIN covers ON horses1._id = covers.stallionName JOIN horses horses2 ON horses2._id = covers.MareName", 
        null); 
    startManagingCursor(coversCursor); 
    // Create an array to specify the fields we want to display in the list 
    // (the renamed fields from the above query) 
    String[] from = new String[] { "stallionNameText", "mareNameText" }; 

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

    // Now create a simple cursor adapter and set it to display 
    SimpleCursorAdapter covers = new SimpleCursorAdapter(this, 
      R.layout.covers_list_row, coversCursor, from, to); 
    setListAdapter(covers); 
} 

然後在onCreate()方法中調用FillData()。然後關閉onDestroy方法中的db。 (選擇涵蓋表中的所有列在這裏看起來很浪費,但我最終會在listView中顯示這些列,我只是想在繼續編碼之前回答這個問題)。 我發現的有用的指南是http://coenraets.org/blog/android-samples/androidtutorial/

我曾與SQL查詢作爲有2個外鍵引用相同表的問題,我想ListView控件同時顯示馬的名字和母馬的名字,所以不得不加入兩匹馬桌子到蓋子桌子。我只需要重命名SQL查詢的FROM部分中的表。希望上面的代碼很清楚。如果不是,我發現以下有用; http://www.bryantwebconsulting.com/blog/index.cfm/2005/3/11/join_a_table_to_itself_in_sql

對不起,如果這種解釋太特殊,我的(不尋常)的例子。 感謝您的閱讀。

9

我是一個新手,Android和SQLLiteHelper,並想知道同樣的事情。

閱讀這篇文章後,我發現,界定方法聯接表之間與SQLLiteQueryBuilder.setTables方法來完成。見reference here

我得到這個從this blog

希望這有助於在正確的方向指向讀者。