Dunkey
Dunkey

Reputation: 1922

Android Sqlite Select particular record

I'm doing some experiment with Android Sqlite. So basically, I'm doing an app which lets users type in some sentences and the app should display the ambiguous words (i.e. words that have multiple meanings). I have a table tblWords (fields are: _id, words, meanings) In each word, there are two meanings like break

For example:

TblWords
_id     words     meanings
1     BREAK     Some abrupt occurrence that interrupts an ongoing activity
2     BREAK     A pause from doing something (as work)

etc...

What I want to do, I want the app to display the only the first records on some certain conditions, otherwise display the second record. How can I achieve something like this? I really need ideas to get me started with. Any help is truly appreciated. And give also some points to consider. Thanks again.

UPDATE:

This is what I've tried so far,

DBHelper.class

  public Cursor getAllWords()
{ 
            return this.myDataBase.query(DB_TABLE, new String[] { 
                    KEY_ID, KEY_WORD, KEY_MEANING }, null, null, null, null, null);

}

MainActivity.class

ArrayList<String> colWords = new ArrayList<String>();
ArrayList<String> colMeanings = new ArrayList<String>();
String[] words;
String[] meanings;

    cursor = dbHelper.getAllWords();

    colWords.clear();///added code
    colMeanings.clear();///added code


    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                colWords.add(cursor.getString(1)); 
                colMeanings.add(cursor.getString(2));   
                String records = cursor.getString(0);

                Log.d("Records", records);
            } while (cursor.moveToNext());
        }
    }

Upvotes: 0

Views: 475

Answers (2)

Matthew Burke
Matthew Burke

Reputation: 2364

Add another column, say definition_number so that your table looks like the following:

_id word definition_number meaning

1 BREAK 1 first definition for break

2 BREAK 2 second definition for break

3 CONTINUE 1 first definition for continue

4 CONTINUE 2 second definition for continue

Now if you want all the first definitions do

this.myDataBase.query(DB_TABLE, new String[] {KEY_ID, KEY_WORD, KEY_MEANING}, 
       "defintion_number = 1", null, null, null, null) 

and if you want all the second definitions do

this.myDataBase.query(DB_TABLE, new String[] {KEY_ID, KEY_WORD, KEY_MEANING}, 
       "defintion_number = 2", null, null, null, null)

I'll add, even though it doesn't address your problem, that you might want to consider separating your data into two tables, Words and Definitions, and then joining as appropriate. Since this is tangential, I won't go into detail, but do a search for schema normalization for more information on why you would want to do this.

Upvotes: 1

Muhammad Ali Hassan
Muhammad Ali Hassan

Reputation: 192

get 2 records and check the condition if true show first record otherwise in else display the second record

Upvotes: 1

Related Questions