Android如何用适当的投影编写查询?

时间:2014-01-01 04:24:56

标签: android sqlite

我有一个名为 TermTable 的sqlite表, id 选择 term type < / i>列:

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";

我按如下方式创建表:

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

“selected”列获取字符串“true”或“false”。

现在我想查询并获取一个字符串作为 term 的游标如果选择列为真,则返回列 id 类型

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;
}

如何定义此查询属性?

3 个答案:

答案 0 :(得分:1)

试试以下内容:

public Cursor getTermValues(int index) {
    String from[] = { "id", "Type" };
    String where = WordsDB.ID + "=?"; // You add column as per your Query
    String[] whereArgs = new String[]{index + ""};
    Cursor cursor = db.query(WordsDB.TermTable, from, where, whereArgs, null, null, null, null);
    return cursor;
}

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

String where = "Selected = '?'"
String whereArgs[] = new String[] { "True" }