2017-12-09 8 views
0

データベースのテーブルから一部のデータを照会するためにselectステートメントを使用しようとしています。 私が入力すると=?私は、%LIKE を使用する場合クエリが成功しますが、%ではなく、私はlogcatでこのエラーました:?SQLite:LIKE '%?%' with rawQueryを使用して選択

FATAL EXCEPTION: main 
Process: com.example.ahmed.bus_time_djerba4, PID: 4178 
java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range. The statement has 0 parameters. 

をして、このデータベースを呼び出す私Methodeのです:

public String QuerySQL(String DepartStation,String Destination){ 

    String result=""; 

    SQLiteDatabase db=this.getReadableDatabase(); 
    Cursor c=db.rawQuery("select distinct * from "+TABLE_Name+" where "+Col_3+" LIKE '%?%' and "+Col_4+" LIKE '%?%'", new String[]{DepartStation,Destination}); 

    if(c.getCount()==0) {result="Data not found";c.close();} 
    else { 
     while (c.moveToNext()) { 
      //affichage des lignes 
      int ligne = c.getInt(1); 
      String Station = c.getString(2); 
      String Dest = c.getString(3); 
      String hours = c.getString(4); 
      result += "\n" + ligne + "|" + Station + "-" + Dest + " " + hours; 
     } 
     c.close(); 
    } 

    return result; 
} 

あるもの問題?してください

+0

私は推測する問題は、+ TABLE_NAME +「から明確な*を選択する 『とある』 ...明確な*?それはする 変更 『私は=?ではなくLIKEの使用時にいや、それは動作しますから...』 – Santosh

+0

選択* '%?%' –

+0

とにかく文字列の連結を使用しないでください。 –

答えて

1

問題は使用しているSQLクエリです。
あなたは与えていますか? prepare文では受け入れられない文字列。 select distinct * from table_name where X like '%?%';は、?が '%' your_string "%"のような引用符で囲まれた文字列になるため、正しくありません。

代わりに書き込み:

select distinct * from table_name where X like ?; 

?使用 "'%your_string%'" のために。これを文字列の配列にも適用できます。

+0

に変更してください。私の場合、このようにする必要があります: DepartStation = "%" + DepartStation + "%"; 宛先= "%" +宛先+ "%"; –

+0

はい、 "%" + DepartStation + "%" ";宛先=" '% "+宛先+"% "" – parsa

+0

作業できません –

関連する問題