Android SQLite - 为什么Column' s字符串返回' null'?

时间:2014-06-08 21:38:48

标签: java android sqlite

这是我的数据库:

private static Database mInstance;
String str;
private static final String TABLE_NAME = "tasks";
protected static final String COLUMN_ID = "_id";
protected static final String COLUMN_TASK = "task_name";
protected static final String COLUMN_EXP = "exp_rate";


private static final String CREATE_TABLE = "CREATE TABLE " 
                            + TABLE_NAME + " (" + COLUMN_ID 
                            + " integer primary key autoincrement, " 
                            + COLUMN_TASK + " TEXT, "+ COLUMN_EXP +" TEXT);"; 
private static final String DATABASE_NAME = "tasks.db";
private static final int DATABASE_VERSION = 1;

我需要获得这个值:' COLUMN_EXP'的但是使用此代码' null'得到。

    public void getTaskExp(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_NAME, new String[] { COLUMN_ID,
            COLUMN_TASK , COLUMN_EXP }, COLUMN_ID + "=?",
    new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor.moveToFirst()) {
        str = cursor.getString(cursor.getColumnIndex(COLUMN_EXP));
    }
     Log.v("hex", "str is " + str);
    cursor.close();
    db.close();
}

我在做什么错误?请帮我。谢谢。对不起我的英语不好。

1 个答案:

答案 0 :(得分:0)

确保表格中有数据。您也可以尝试在列中检查null。

之类的,

if(str != null){
  Log.v("hex", "str is " + str);

}

如果仍然存在,请检查一下。 SQLite Query returns null