2016-11-29 7 views
0

私はアンドロイドでSQLiteと対戦しており、いくつかの問題が解決しました。問題はherehereです。selectionArgsエラーはカラムが存在しないと言います

ここで、特定の状態に対してDISTINCT領域を選択します。私の最初のRecyclerView画面には、次のコードで状態が正常に表示されます。

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) 

私はを選択を今すぐ

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

は、私は次のコードを使用状態ごとに別個の領域を取得しますタスマニア
リストから タスマニアが文字列引数 として渡されましたwhichState

私は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; 
} 
関連する問題