【问题标题】:Get value of the Foreign Key in SimpleCursorAdapter获取 SimpleCursorAdapter 中外键的值
【发布时间】:2017-05-08 12:47:26
【问题描述】:

我有一个 listView 填充了我的数据库中的数据。我使用 simpleCursorAdapter 来显示值。

我有一张桌子可以添加课程:英语、法语...

在另一个表中,我可以创建已开发的课程(我添加了开始和结束的日期、哪几天、课程的主题)。我必须以 FK 的身份提供课程。

当我添加课程时,我想在我的列表视图中显示每个示例:英语 - 阅读,但它显示 1 - 阅读。因为 1 是我存储在第二张表中的值。

如何将 1 改为英文?

这是我的代码:

    Cursor cursor = dbhelper.getAllCours();
    String[] from = { "branche_cours", "designation" }; //here 'branche_cours' is the lesson i store as an INT, it's the FK so
    int[] to = { R.id.text_branche_cours, R.id.text_designation };
    adapter = new SimpleCursorAdapter(this, R.layout.list_row, cursor, from, to, 0);
    lvCours.setAdapter(adapter);
    adapter.notifyDataSetChanged();

我使用getAllCours()的方法

public Cursor getAllCours()
{

    //from here, i retrieve the ID, designation and branche_cours
    String Query = ("select ID as _id, date_debut, date_dernier, dixieme_point, " +
            "demi_point, description, designation, lundi, mardi, mercredi, jeudi," +
            " vendredi, samedi, branche_cours from " + TABLE_COURS);

    Open();

    Cursor cursor = db.rawQuery(Query, null);

    return cursor;
}

如何将该 int 链接到实际值(那么 '1' 怎么能变成 'English')?

【问题讨论】:

  • 我还没有找到解决办法..

标签: android sqlite listview foreign-key-relationship


【解决方案1】:

一种解决方案是执行SQL JOIN 操作以从两个表中获取数据:

所以 SQL 查询应该是这样的:

SELECT table1.branche_cours, table2.designation 
FROM table1
INNER JOIN table2 ON table1.ID=table2.ID; 

【讨论】:

  • 我就是这样测试的,成功了,谢谢你的帮助!
【解决方案2】:

要在另一个表中查找值,您可以使用correlated subquery

SELECT ID AS _id,
       ...,
       samedi,
       (SELECT name
        FROM other_table
        WHERE other_table.id = cours.branche_cours
       ) AS branche_cours
FROM cours;

【讨论】:

    猜你喜欢
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多