1

MainActivity.java:

    private void displayListView() {
    cursor = myDatabase.getJoinedInfo(etSearch.getText().toString());

    String[] columns = new String[] { myDatabase.RE_VALUE,
            myDatabase.GL_VALUE, myDatabase.KE_VALUE };

    int[] to = new int[] { R.id.tvHiragana, R.id.tvMeaning, R.id.tvKanji };

    dataAdapter = new SimpleCursorAdapter(this, R.layout.wordonlist,
            cursor, columns, to, 0);

    ListView listView = (ListView) findViewById(R.id.lvWordlist);
    // Assign adapter to ListView

    listView.setAdapter(dataAdapter);
}

MyDatabase.java:

public static final String GL_TABLE="gloss";
public static final String GL_ID="id";
public static final String GL_LANG="lang";
public static final String GL_VALUE="value";

public static final String KE_ID="id";
public static final String KE_VALUE="value";

public static final String RE_ID="id";
public static final String RE_VALUE="value";

public static final String LOG_TAG="myLogs";

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

    SQLiteDatabase db=getReadableDatabase();

    Cursor cursor;
    String query;

    query="SELECT " +
            " e.id AS _id," +
            " ke.id AS _id," +
            " ke.fk," +
            " ke.value," +
            " re.id AS _id," +
            " re.fk," +
            " re.value," +
            " s.id AS _id," +
            " s.fk," +
            " g.id AS _id," +
            " g.fk," +
            " g.lang," +
            " g.value" +
            " FROM entry e" +
            "     INNER JOIN k_ele ke ON e.id = ke.fk" +
            "     INNER JOIN r_ele re ON e.id = re.fk" +
            "     INNER JOIN sense s ON e.id = s.fk" +
            "     INNER JOIN gloss g ON s.id = g.fk" +
            " WHERE g.value like '%"+lookingFor+"%' LIMIT 20";

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

    return cursor;      
}

私は5つのテーブルを持っています。そのうちの 3 つには、「値」という名前の列があります。カーソルは、1 つのテーブルの「値」のみを取得し、他のテーブルを無視しています。そして、検索文字列に一致するすべての値は類似しています! この問題を解決する方法について何か提案をお願いします。

ここに画像の説明を入力

4

1 に答える 1

2

キーワードの要点はAS、取得されたすべての列の中で一意の別の名前を列に割り当てることです。それらの一部でのみ使用しており、常に同じ列名を割り当てています。クエリは次のようになります

query = "SELECT " +
        " e.id AS e_id," +
        " ke.id AS ke_id," +
        " ke.fk as ke_fk," +
        " ke.value as ke_value," +
        " re.id AS re_id," +
        " re.fk as re_fk," +
        " re.value as re_value," +
        " s.id AS s_id," +
        " s.fk as s_fk," +
        " g.id AS g_id," +
        " g.fk as g_fk," +
        " g.lang," +
        " g.value as g_lang" +
        " FROM entry e" +
        "     INNER JOIN k_ele ke ON e.id = ke.fk" +
        "     INNER JOIN r_ele re ON e.id = re.fk" +
        "     INNER JOIN sense s ON e.id = s.fk" +
        "     INNER JOIN gloss g ON s.id = g.fk" +
        " WHERE g.value like '%"+lookingFor+"%' LIMIT 20";

そうすれば、結果セットのすべての列に異なる名前が付けられます。g、s、e、ke、re などの名前は意味がなく、コードを読みにくく、理解しにくくするため、ネーミングも改善する必要があります。

于 2013-08-11T08:16:11.963 に答える