0

I have an android application which has a small database as a db file in the asset folder. The file is called nametoareamap.db.It has a single table named 'map'. The table has two columns(Names and Areas) as following:

Names       Areas

Aaron        A

Chris        A 

Helen        B 

Tim          B

My application takes names as input from the user. Suppose some user had input: Aaron, Tim. In this case, in terms of name there are two matches with the database. But they come from different areas. Aaron from A and Tim from B. I want to implement the following logic.

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

{ i take a decision}

else

 {i decide something else }

Can anyone kindly provide me the code required to do this with cursor and sqlite databases on Android. I have a database adapter already.Thanks in advance

2
  • pls more explanation, you want set and get data from your table or what ?? Commented Aug 10, 2012 at 11:20
  • hi i want to get data from the table... Commented Aug 11, 2012 at 4:25

1 Answer 1

1

Assuming the following table layout

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

And the following values

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

Say you want to know if Aaron, Ron and Burgundy all are in the same area or not:

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

This would return two rows.

2   A
1   B

i.e. two of the are in the same area (A), one is in another (B):

Expressed as a Cursor you could check it like this:

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.