selectionArgs错误表示列不存在

时间:2016-11-29 03:02:49

标签: android sqlite android-sqlite

所以我一直在使用Android上的SQLite进行摔跤并解决了一些问题。问题包括herehere

我现在想为给定的状态选择DISTINCT区域。我的第一个RecyclerView屏幕使用以下代码显示状态。

public Cursor getStateData() {
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    String [] sqlSelect = {"0 _id", "state" };
    String sqlTables = "Reefs";
    qb.setTables(sqlTables);
    qb.setDistinct(true);
    Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);
    c.moveToFirst();
    return c;
}

现在为每个州获取DISTINCT区域,我使用以下代码:

public Cursor getRegionData(String whichState) {
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    String [] sqlSelect = {"0 _id", "region" };
    String theState = whichState;
    String sqlTables = "Reefs";
    qb.setTables(sqlTables);
    qb.setDistinct(true);
    Cursor c = qb.query(db, sqlSelect, theState , null , null, null, null);
    //  (table, columms to return,  colums for the where clause,  values for the where clause, null, null, null)
    c.moveToFirst();
    return c;
}

它不起作用并给我这个错误信息:

Caused by: android.database.sqlite.SQLiteException: no such column: Tasmania (code 1): , while compiling: SELECT DISTINCT 0 _id, region FROM Reefs WHERE (Tasmania)

我从列表中选择了塔斯马尼亚,因此塔斯马尼亚作为字符串参数 whichState 传入。

因此,如果我用纯粹的SQL术语来描述问题,那就是这样的:

SELECT
  region  //column in my table called region
FROM
  Reefs  // table in my database
WHERE
  state = [the state selected]  //column in my table called **state** which I pass in with the varible **whichState**

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您传递的是值,而不是WHERE条款,请参阅documentation

要更正它,请传递WHERE子句和参数:

public Cursor getRegionData(String whichState) {
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    String [] sqlSelect = {"0 _id", "region" };
    String [] theState = { whichState };
    String sqlTables = "Reefs";
    qb.setTables(sqlTables);
    qb.setDistinct(true);
    Cursor c = qb.query(db, sqlSelect, "state=?", theState, null, null, null);
    c.moveToFirst();
    return c;
}

答案 1 :(得分:0)

323go完美地挑选了它。这是我的完整代码,因为它可以帮助其他人解决同样的问题。

有趣的是,我将最后一个方法 getAreaData 整理得很好而且略有不同很高兴能正确使用该代码,因为我是sql的新手。

public Cursor getStateData() {
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    String [] sqlSelect = {"0 _id", "state" };
    String sqlTables = "Reefs";
    qb.setTables(sqlTables);
    qb.setDistinct(true);
    Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);
    c.moveToFirst();
    return c;
}

public Cursor getRegionData(String whichState) {
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    String [] sqlSelect = {"0 _id", "region" };
    String [] theState = { whichState };
    String sqlTables = "Reefs";
    qb.setTables(sqlTables);
    qb.setDistinct(true);
    Cursor c = qb.query(db, sqlSelect, "state=?" , theState, null, null, null);
    //  (table, columms to return,  colums for the where clause,  values for the where clause, null, null, null)

    c.moveToFirst();
    return c;
}

public Cursor getAreaData(String whichState, String whichRegion) {
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    String [] sqlSelect = {"0 _id", "state", "region", "area", "latitude", "longitude"};
    String sqlFilter = "state= '"+whichState+"' AND region= '"+whichRegion+"'";
    String [] sqlValues = {""};
    String sqlTables = "Reefs";
    qb.setTables(sqlTables);
    qb.setDistinct(true);
    Cursor c = qb.query(db, sqlSelect, sqlFilter, null, null, null, null);
    c.moveToFirst();
    return c;
}