0

アセットフォルダにdbファイルとして小さなデータベースがあるAndroidアプリケーションがあります。このファイルはnametoareamap.dbと呼ばれ、「map」という名前の単一のテーブルがあります。テーブルには、次の2つの列(名前と領域)があります。

Names       Areas

Aaron        A

Chris        A 

Helen        B 

Tim          B

私のアプリケーションは、ユーザーからの入力として名前を受け取ります。アーロン、ティムなどのユーザーが入力したとします。この場合、名前に関しては、データベースとの一致が2つあります。しかし、彼らはさまざまな地域から来ています。AのAaronとBのTim。次のロジックを実装したいと思います。

If match > = 2 && the area of the matches are same

{ i take a decision}

else

 {i decide something else }

Androidのカーソルデータベースとsqliteデータベースでこれを行うために必要なコードを誰かが親切に提供してくれますか?すでにデータベースアダプタを持っています。よろしくお願いします。

4

1 に答える 1

1

次のテーブル レイアウトを想定しています。

CREATE TABLE name_area (
    _id INTEGER PRIMARY KEY NOT NULL,
    name TEXT NOT NULL,
    area TEXT NOT NULL,
    UNIQUE(name, area)
)

そして、次の値

name      area
----      ----
Aaron     A
Chris     A
Bunny     A
Ron       A
Burgundy  B
Helen     B 
Tim       B

Aaron、Ron、Burgundy がすべて同じエリアにいるかどうかを知りたいとします。

SELECT COUNT(*), area FROM name_area 
    WHERE name='Aaron' OR name='Ron' OR name='Burgundy' GROUP BY area

これにより、2 つの行が返されます。

2   A
1   B

つまり、2 つが同じエリアにあり (A)、1 つが別のエリアにある (B):

として表現すると、次のCursorように確認できます。

Cursor cursor = ...; // Format your query & do the SELECT
try {
    if (cursor.moveToNext()) {
        int count = cursor.getCount();
        if (count < 2) {
            // Everyone is in the same area
            int n = cursor.getInt(0);
            // Now verify 'n' against the number of people you queried for
            // if it doesn't match one or more didn't exist in your table.
        } else {
            // People are in different areas
            int n = 0;
            do {
               n += cursor.getInt(0);
            } while (cursor.moveToNext());
            // Now verify 'n' against the number of people you queried for
            // if it doesn't match one or more didn't exist in your table.
        }
    } else {
        // Oops nothing was found.
    }
} finally {
    cursor.close();
}
于 2012-08-10T12:54:34.653 に答える