Ali Allahyar
Ali Allahyar

Reputation: 341

Android how to get values from columns in a sqlite database

I have a sqlite table called TermTable with the columns id, term and type:

static final String TermTable = "Terms";
static final String ID = "id";
static final String Term = "term";
static final String Type = "type";

Also, I have a Datasource class that implements the db and have the following methods to put and get data in and out of table:

public long insertTermList(String term, String type) {

    ContentValues initialValues = new ContentValues();

    initialValues.put(WordsDB.Term, term);
    initialValues.put(WordsDB.Type, type);

    return db.insert(WordsDB.TermTable, null, initialValues);
}

public Cursor getTermValues(int index) {

    String from[] = { "Term", "Type" };
    String where = WordsDB.ID + "=" + index;
    Cursor cursor = db.query(true, WordsDB.TermTable, from, where, null, null, null, null, null);
    return cursor;
}

In another class I have invoked the insertTermList and inserted some for rows of values using a Datasource object called ds:

private void setData()
{
    ds.insertTermList("abbey", "noun");
    ds.insertTermList("abide", "verb");
    ds.insertTermList("abound", "verb");
    ds.insertTermList("absurd", "adjective");
}

Now I want to get values from the columns according to the id I give it and append each column value of each row to a text view called text. How can I do that?

private void getData() {

    Cursor c = ???

    if(c != null)
    {
        c.moveToFirst();
        text.append(???);
    }
}

Any suggestions?

Upvotes: 0

Views: 19311

Answers (1)

Chintan Soni
Chintan Soni

Reputation: 25267

So, first of all, change your following function:

public Cursor getTermValues(int index) {

    String from[] = { "Term", "Type" };
    String where = WordsDB.ID + "=" + index;
    Cursor cursor = db.query(true, WordsDB.TermTable, from, where, null, null, null, null, null);
    return cursor;
}

to

public Cursor getTermValues(int index) {

    String from[] = { "Term", "Type" };
    String where = WordsDB.ID + "=?";
    String[] whereArgs = new String[]{index+""};
    Cursor cursor = db.query(WordsDB.TermTable, from, where, whereArgs, null, null, null, null);
    return cursor;
}

then,

    private void getData(int id) {

        Cursor c = getTermValues(id);

        if(c != null)
        {
            while(c.moveToNext){
                String term = c.getString(c.getColumnIndex("term")));
                String type = c.getString(c.getColumnIndex("type")));


                // use these strings as you want
            }
        }
    }

if you want to ge all the records then create a method:

public void getAllRecords() {

    Cursor cursor = db.query(WordsDB.TermTable, null, null, null, null, null, null, null);
    if(c != null)
            {
                while(c.moveToNext){
                    String term = c.getString(c.getColumnIndex("term")));
                    String type = c.getString(c.getColumnIndex("type")));


                    // use these strings as you want
                }
            }
}

Upvotes: 3

Related Questions