Ali Allahyar Ali Allahyar - 23 days ago 9
Android Question

Android how to write the query with a proper projection?

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

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


I create the table as follows:

private static final String TERM_TABLE_CREATE = "Create table " + TermTable +
"(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
Selected + " VARCHAR(5) ," +
Term + " VARCHAR(20) ," +
Type + " VARCHAR(10))";


The column "selected" gets the strings "true" or "false".

Now I want to query and get the cursor which gets a string as a term and if its selected column is true, returns the columns id and type:

public Cursor getFavoriteValues(String term)
{
String from[] = { "id", "type" };
String where = ???
String whereArgs[] = ???
Cursor cursor = db.query(WordsDB.TermTable, from, where, whereArgs, null, null, null, null);
return cursor;
}


How are this query attributes defined?

Answer
public Cursor getTermValues(String term)
{
    String from[] = { "id", "Type" };
    String where = WordsDB.Selected + "=? AND " + WordsDB.Term + "=?";
    String whereArgs[] = new String[]{ "true", term };
    Cursor cursor = db.query(WordsDB.TermTable, from, where, whereArgs, null, null, null, null);
    return cursor;
}
Comments