0

现在我有以下代码:

    SQLiteDatabase db=getReadableDatabase();
    SQLiteQueryBuilder qb=new SQLiteQueryBuilder();

    String[] sqlSelect={"0 _id","lang","value"};
    String sqlTables="gloss";

    qb.setTables(sqlTables);

    Cursor c=db.query(GL_TABLE, sqlSelect, "value like '%"+inputText+"%'", null, null, null, null, "20");

它工作完美。这里这段​​代码只占用一张桌子。如何从相互关联的 3 个表中获取数据(entry 是主节点,sense 是 entry 的子节点,gloss 是 sense 的子节点):

条目(id,ent_seq);感觉(id,fk);光泽度(id,fk,lang,值);

是否可以使用 setTables 做到这一点?如果是,请给出一些建议如何做到这一点。

我已经按照@Anders8 所说的那样做了:

public Cursor getJoinedInfo(String lookingFor)
{
    Log.d(LOG_TAG, "DB: looking up info");

    SQLiteDatabase db=getReadableDatabase();

    Cursor cursor;
    String query;

    query="SELECT " +
            " e.id," +
            " s.id," +
            " s.fk," +
            " g.id," +
            " g.fk," +
            " g.lang," +
            " g.lang" +
            " FROM entry e" +
            "     INNER JOIN sense s ON e.id = s.fk" +
            "     INNER JOIN gloss g ON s.id = g.fk" +
            " WHERE g.value like '%"+lookingFor+"%'";

    Log.d(LOG_TAG, "DB: query = \n" + query.replace(", ",",\n  "));
    cursor=db.rawQuery(query,null);
    Log.d(LOG_TAG, "DB: query complete");

    return cursor;      
}

它给出了这个错误:

08-09 22:26:16.632: E/AndroidRuntime(9842): java.lang.IllegalArgumentException: 列 '_id' 不存在

我应该怎么办?

4

2 回答 2

3

这将是这样的:

public Cursor getJoinedInfo(String lookingFor)
{
    Log.d(TAG, "DB: looking up info");

    Cursor cursor;
    String query;

    query="SELECT " +
            " a.fieldName1," +
            " a.fieldName2," +
            " b.fieldName3," +
            " c.fieldName4" +
            " FROM tableName1 a" +
            "     INNER JOIN tableName2 b ON a.someKeyInA = b.someKeyInB" +
            "     INNER JOIN tableName3 c ON a.someOtherKeyInA = c.someKeyInC" +
            " WHERE a.FieldName ='" + lookingFor +"'";
    Log.d(TAG, "DB: query = \n" + query.replace(", ",",\n  "));
    cursor=db.rawQuery(query,null);
    Log.d(TAG, "DB: query complete");

    return cursor;      
}

显然,构建您需要的查询。;)

于 2013-08-08T17:22:59.810 回答
-1

您可以在 SQLite 中执行连接。这将允许您将正在使用的表中的所有或仅某些属性连接到一个最终关系中。这样您就可以准确地选择您要查找的内容。

我不知道数据集中有什么或您在寻找什么,所以我无法编写查询,但我会这样说 - 您最好的方法是 (1) 将查询编写为字符串 (2) 执行原始数据库使用该字符串进行查询,(3) 使用光标从结果中选择数据。

于 2013-08-08T16:29:34.723 回答