游标返回null

时间:2012-07-24 15:15:30

标签: android sqlite cursor

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。有什么帮助吗?

3 个答案:

答案 0 :(得分:4)

使用SQLiteDatabase查询方法代替rawQuery以获得最佳效果。

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

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

答案 1 :(得分:3)

尝试这种方式:

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

答案 2 :(得分:2)

  Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_MOVIES
            + " WHERE name LIKE "+searchName, null); // Put Like When your are comparing String