0

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?

0

1 Answer 1

3

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

8 Comments

So, what is the range of ids I can feed for getData method? I tried 0 and nothing was printed and tried 1 and I got IllegalStateException get field slot from row 0 col -1 faild.
Do you want to display all the records ?
yes, all the column values of each row I inserted. you know how?
Still get IllegalStateException get field slot from row 0 col -1 faild. Have any idea why this is happening?
Please post your full activity code and logcat errors, i think your database is not open while you are querying database.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.