-1

以下のコードに文字列を渡していますが、エラーが発生します:

Error: 08-24 12:57:55.583: E/AndroidRuntime(11640): android.database.sqlite.SQLiteException:    near "RAMDEV": syntax error: , while compiling: SELECT chnno FROM EPG WHERE title = BABA RAMDEV KA YOGA

私のコードはここにあります:

public String SearchChnNo(String title){

    String selectQuery = "SELECT " + chn_no + " FROM " + EPG + " WHERE " + pgm_title + " = " + title ; //error

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    String chnno = cursor.getString(1);
    return chnno;

}
4

4 に答える 4

1

Lalit Poptaniの答えは正しいですが、次のように、生のクエリメソッドの選択引数を使用することをお勧めします。

クエリ内に値を書き込む必要があるたびに、代わりに疑問符を記述してください。

次に、文字列の配列内に値自体を追加する必要があります。

あなたの場合、それはこのようになります:

public String SearchChnNo(String title){

    String selectQuery = "SELECT " + chn_no + " FROM " + EPG + " WHERE " + pgm_title + " = ? " ;
    String selectionArgs [] = new String [] { title };

    SQLiteDatabase db = this.getReadableDatabase ();
    Cursor cursor = db.rawQuery ( selectQuery , selectionArgs );
    // The default value, if none is found
    String chnno = "N/A";
    if ( cursor.moveToFirst () )
        chnno = cursor.getString ( 1 );
    // Do not forget to close the cursor
    cursor.close ();
    cursor = null;
    return chnno;

}
于 2012-08-24T07:45:02.157 に答える
0

期待しているのは、一重引用符で囲む必要のpgm_titleある文字列です。title

pgm_title + " = '" + title+"'"

したがって、最終的なクエリは次のようになります。

String selectQuery = "SELECT " + chn_no + " FROM " + EPG + " WHERE " + 
                                             pgm_title + " = '" + title+"'" ;
于 2012-08-24T07:38:50.940 に答える
0

LeeeeeloとLalitの両方の回答に追加するには、代わりに実際に行うべきことは次のとおりです。

SQLiteDatabse db = this.getReadableDatabase();
Cursor cursor = db.query(EPG, new String[] {chn_no}, ""+pgm_title+"=?", new String [] { title}, null, null, null);
cursor.moveToFirst(); // that assumes there are results, you should probably make sure this returns true
String chnno = cursor.getString(1);
cursor.close();
return chnno;
于 2012-08-24T09:12:32.840 に答える
0
   public String SearchChnNo(String title){

    String chnno = null;

   String selectQuery = "SELECT " + chn_no + " FROM " + EPG + " WHERE " + pgm_title + "   = '" + title + "'" ;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);


    if (cursor.moveToFirst()) {
        do {
            chnno = cursor.getString(cursor.getColumnIndex(chn_no));
            //chnno = cursor.getString(chn_no);
        } while (cursor.moveToNext());
    }

    // closing connection
    cursor.close();
    db.close();

    // returning
    return chnno;

  }
于 2012-08-25T06:44:22.387 に答える