2016-11-29 46 views
0

所以我一直在與Android上的SQLite摔跤,並已解決了一些問題。問題是herehereselectionArgs錯誤表示列不存在

我現在想爲給定狀態選擇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; 
} 

我們得到不同的區域,因爲我使用下面的代碼每個狀態:

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) 

我選擇塔斯馬尼亞島從列表中如此塔斯馬尼亞作爲字符串參數傳入其中狀態爲

所以,如果我是來描述purly 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** 

我在做什麼錯?

+0

你爲什麼選擇'0'(常量)? –

+0

我正在使用SQLiteAssetHelper和SQLite的新手使用這些傢伙的解決方案:https://github.com/jgilfelt/android-sqlite-asset-helper – timv

回答

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

謝謝,正是我的問題,現在解決了。欣賞你的時間回答如此之快。 – timv

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