2
public String getContact(String searchName) {
    SQLiteDatabase db = this.getReadableDatabase();

    String[] args = { searchName };

    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_MOVIES
            + " WHERE name =? ", args);

    String iName = null, iDiretor = null, iGenre = null;

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        iName = cursor.getString(cursor.getColumnIndex(KEY_NAME));
        iDiretor = cursor.getString(cursor.getColumnIndex(KEY_DIRECTOR));
        iGenre = cursor.getString(cursor.getColumnIndex(KEY_GENRE));
        cursor.moveToNext();
    }

    cursor.close();

iName 变量工作正常,但其他两个返回 null。有什么帮助吗?

4

3 回答 3

4

使用SQLiteDatabase查询方法而不是rawQuery获得最佳结果。

db.query(TABLE_MOVIES, null, "name = ?", args, null);

这是首选,因为rawQuery它很容易搞砸并且不能防止 SQL 注入。

于 2012-07-24T15:19:48.380 回答
2

试试这个方法:

Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_MOVIES + " WHERE name LIKE ? ", args);
于 2012-07-24T15:19:00.023 回答
2
  Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_MOVIES
            + " WHERE name LIKE "+searchName, null); // Put Like When your are comparing String
于 2012-07-24T15:19:30.930 回答