簡體   English   中英

android sqlite數據庫游標

[英]android sqlite database cursor

我有一個android應用程序,該應用程序在資產文件夾中有一個小的數據庫作為db文件。 該文件名為nametoareamap.db。它具有一個名為“ map”的表。 該表具有兩列(名稱和區域),如下所示:

Names       Areas

Aaron        A

Chris        A 

Helen        B 

Tim          B

我的應用程序將名稱作為用戶的輸入。 假設有用戶輸入:Aaron,Tim。 在這種情況下,就名稱而言,數據庫有兩個匹配項。 但是它們來自不同的領域。 來自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數據庫執行此操作所需的代碼。 我已經有一個數據庫適配器。謝謝

假設下面的表格布局

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

假設您想知道亞倫,羅恩和勃艮第是否都在同一地區:

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

這將返回兩行。

2   A
1   B

也就是說,其中兩個位於同一區域(A),一個位於另一個區域(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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM