2016-08-27 207 views
0

所以我有一個數據庫的表有一個列(COLUMN_ID,COLUMN_TARGET,COLUMN_CURRENT)。我正在嘗試檢索COLUMN_ID = 1列的COLUMN_TARGET列的值,並將其保存在變量tar中並將其返回,但當我運行它時,我的應用程序不斷崩潰。我使用日誌消息來計算出錯位置,並打印出「1 ...」,然後崩潰。android sqlite從數據庫檢索值?

public int getTarget() { 

    SQLiteDatabase db = this.getReadableDatabase(); 
    String[] columns = {COLUMN_TARGET}; 
    int tar=0; 
    Log.d("tag","1...\n"); 
    Cursor c = db.query(TABLE_VALUES, columns, COLUMN_ID+"=?", new String[] { String.valueOf(1) }, null, null, null,null); 
    Log.d("tag","2...\n"); 
    if (c.getCount() == 1) { 
     Log.d("tag","3...\n"); 
     c.moveToFirst(); 
    } 
    Log.d("tag","4...\n"); 
     tar = c.getInt(0); 
    Log.d("tag","5...\n"); 
    db.close(); 
    c.close(); 
    Log.d("tag","6...\n"); 
    // return Integer.parseInt(tar); 
    return tar; 

} 

有人可以解決這個問題嗎?在此先感謝

回答

0

嘗試以下:

String SELECT_SQL = "SELECT * FROM "+ TABLE_VALUES + "WHERE COLUMN_ID =" + 1 + ";"; 
    Cursor c = db.rawQuery(SELECT_SQL, null); 
    if (c.getCount() == 1) { 
     Log.d("tag","3...\n"); 
     c.moveToFirst(); 
     int tar = c.getInt(0); 
    }