安卓SQLite联接查询

3

我正在创建一个作为学习工具的应用程序,并且在连接查询方面遇到了困难。

我有两个表格的数据库 - horses和covers- 声明如下:

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

'type'字段指公马、母马、阉马等,并从一个微调器中进行选择(由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字段中存储的。它是从一个下拉菜单中选择的,该下拉菜单只显示类型定义为“Stallion”的马在马表中。(Mare也适用相同的方式)。

我有一个名为“DatabaseHelper”的类来创建和升级表,每个表都有自己的适配器类'horsesDbAdapter'和'coversDbAdapter',包含添加、编辑和删除条目以及相关查询的方法。(fetchAllHorses()、fetchHorse(long 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记事本示例中改编的)
我在ListView中显示了“封面表”中的内容(仅显示公马名称和母马名称)。但由于这些字段只包含对“马匹表”中唯一引用,因此显示的只是相当不具信息性的_id字段。
我的问题是:如何在ListView中获取要显示的马的相关名称?我已经阅读了联接查询等文档,但在尝试实现它们时迷失了。我假设我需要连接horses._id和covers.stallionName(然后再为MareName创建一个几乎相同的)但我找不到如何执行此操作的具体示例。
如果需要任何其他信息/代码,请告诉我。
非常感谢您提供的任何帮助。谢谢!
编辑:我已经将stallionName和mareName设置为引用“马匹表”中的(_id)外键,但仍不确定在哪里以及如何实现联接查询;应该放在horsesDbAdapter、coversDbAdapter或coversList类中?(coversList是创建和填充ListView的类) 现在,covers表的声明如下:
    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));";

你应该在covers表中创建一个外键,引用到horses表的相关列。 - Simon Dorociak
@deceiver 感谢您的回复。我已经将它添加到了covers表的声明中,但我仍然不确定如何实现连接以及如何让它填充listView。 - Bruce0
2个回答

9
我是一名Android和SQLLiteHelper的新手,对此有些疑问。
阅读本文后,我发现定义表之间连接的方法是使用SQLLiteQueryBuilder.setTables方法。请参见此处的参考文档。
我从这篇博客中了解到了这个方法。
希望这能帮助读者找到正确的方向。

2
我已经成功使其工作。对于其他可能遇到类似问题的人,我将尝试详细说明我所做的事情。 正如 @deceiver 所述,我应该将 stallionName 和 mareName 作为外键引用 horses(请参见编辑)。
在 coversList 类中(实现 listView 的类),我只需要获取数据库的实例并使用 rawQuery 直接实现 SQL 代码即可(也许可以使用 Query,但我不确定如何操作)。 新增的代码如下:
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方法中关闭数据库。 (在这里选择从covers表选择所有列似乎是浪费的,但我最终将在listView中显示这些列。我只是想在继续编码之前回答这个问题)。 我发现以下教程很有帮助:http://coenraets.org/blog/android-samples/androidtutorial/

由于有2个外键引用同一张表,并且我想让listView同时显示公马名和母马名,所以我在两个horses表格与一个covers表格进行了连接。我只需要在SQL查询的FROM部分重命名表格即可。希望以上代码清晰明了。如果不是,请参考以下链接:http://www.bryantwebconsulting.com/blog/index.cfm/2005/3/11/join_a_table_to_itself_in_sql

如果这个解释对我的(不寻常的)例子过于具体,那么很抱歉。 感谢您的阅读。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接