2017-02-19 31 views
0

我有一個查詢(getMinScore),它從我的表中取最小值,我需要這一點,並在文本字段,但它顯示它的當前狀態,它會顯示返回SQLite的查詢和返回文本

High Score: [email protected] 

我不知道這是什麼意思,我怎麼能從這個字符串值?

我在我的GameView類中調用查詢,就像在第二個代碼塊中那樣。真的很感謝任何幫助!

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper { 

public static final String DATABASE_NAME = "scores.db"; 
public static final String TABLE_NAME = "scores_table"; 
public static final String COLUMN_ID = "ID"; 
public static final String COLUMN_SCORE = "SCORE"; 

public DatabaseHelper(Context context) { 
    super(context, DATABASE_NAME, null, 1); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT, SCORE INTEGER)"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
    onCreate(db); 
} 

public boolean insertData(String score) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COLUMN_SCORE, score); 
    long result = db.insert(TABLE_NAME, null, contentValues); 
    System.out.println("Data inserted" + score); 

    if(result == -1) { 
     return false; 
    } 
    else { 
     return true; 
    } 

} 

public Cursor getAllData() { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor res = db.rawQuery("select * from " + TABLE_NAME, null); 
    return res; 
} 

public Cursor getMinScore() { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor res = db.rawQuery("select min (SCORE) from " + TABLE_NAME, null); 
    return res; 
} 

public boolean updateData(String id, String score) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COLUMN_ID, id); 
    contentValues.put(COLUMN_SCORE, score); 
    db.update(TABLE_NAME, contentValues, "ID = ?", new String[] { id }); 
    return true; 
} 

public Integer deleteData(String id) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    return db.delete(TABLE_NAME, "ID = ?", new String[] { id }); 
} 
} 

GameView.java

String minScore = mydb.getMinScore().toString(); 
TextPaint tp = new TextPaint(); 
tp.setColor(Color.GREEN); 
tp.setTextSize(40); 
tp.setTypeface(Typeface.create("Courier", Typeface.BOLD)); 
canvas.drawText("Moves: " + String.valueOf(turns), 10, 1180, tp); 
canvas.drawText("High score: " + mydb.getMinScore(), 10, 1280, tp); 
+0

告訴我們所有的錯誤,請 –

+0

這就是我所看到的,其餘的顯示顏色的固體塊之後。 – jb2002

回答

0

一個Cursor是一個對象,讓你通過循環行訪問數據庫查詢的結果。你不能只打印光標,你需要檢查是否有下一行,然後從中取出數值。

你可以改變你getMinScore方法返回MinScore是價值,而不是返回光標:

public int getMinScore() { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor res = db.rawQuery("select min (SCORE) from " + TABLE_NAME, null); 

    if(res.moveToNext()) { 
     // return the integer from the first column 
     return res.getInt(0); 
    } else { 
     // there were no results 
     return -1; 
    } 
} 

documentation的讀,瞭解光標的方法。

+0

'min()'總是返回一個值。 –

0

您必須從返回的Cursor對象中獲取您的值。在你的情況下,你可以使用

int score = res.getInt(res.getColumnIndex("SCORE")); 

確保通過檢查光標計數> ZERO來檢查返回的光標不是NULL。

更改方法

public Cursor getMinScore() { 
SQLiteDatabase db = this.getWritableDatabase(); 
Cursor res = db.rawQuery("select min (SCORE) from " + TABLE_NAME, null); 
return res; 

}

public int getMinScore() { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor res = db.rawQuery("select min (SCORE) from " + TABLE_NAME, null); 
    if (res.getCount() > 0) { 
     return res.getInt(res.getColumnIndex("SCORE")); 
    } else { 
     return -1; // Some defualt value when no data is retrieved 
    } 
} 
+1

'min()'總是返回一個值。 –

+0

我加了檢查只是爲了避免這個例外。如果有的話。 –

+0

什麼例外?沒有任何錶行,'min()'會返回一個NULL值的結果行,'getInt()'將轉換爲'0'。 –

0

你應該總是關閉遊標,以防止內存泄露

所以

public int getMinScore() { 
     int result = -1; 

     Cursor res = db.rawQuery("select min (SCORE) from " + TABLE_NAME, null); 

     if (res.moveToFirst()) { 
      result = res.getInt(0); 
     } 

     if (!res.isClosed()){ 

      res.close(); 
     } 
     return result; 
    } 
} 

我認爲最好用戶的方法打開和關閉,而不是調用getWritableDatabase()每次

public void open() { 
    bdd = dbHelper.getWritableDatabase(); 
} 

public void close() { 
    bdd.close(); 
}