想要在表單中新加入一行然後確認是否加入正確,等到查詢的時候,程序就崩潰了。
addNewGame(1);
Cursor cursor2 = db.query(myDBOpenHelper.GAMES_TABLE, new String [] {"MAX("+GAME_COLUMN+") AS GC"}, null, null, null, null, null);
System.err.println("made it this far");
String temp = cursor2.getString(cursor.getColumnIndex("GC")); //crash!!!
System.err.println("didn't get here);
private static void addNewGame(int g) {
ContentValues newValues = new ContentValues();
SQLiteDatabase db = DBOpenHelper.getWritableDatabase();
newValues.put(GAME_COLUMN, g);
newValues.put(CIV_COLUMN, "");
db.insert(myDBOpenHelper.GAMES_TABLE, null, newValues);
}
thread exiting with uncaught exception
創建表單的代碼:
private static final String GAMES_CREATE = "create table " +
GAMES_TABLE + " (" + KEY_ID +
" integer primary key autoincrement, " +
GAME_COLUMN + " text not null, " +
CIV_COLUMN + " text);";
db.execSQL(GAMES_CREATE);
db.query(myDBOpenHelper.GAMES_TABLE, new String [] {"MAX("+GAME_COLUMN+") AS GC"}, null, null, null, null, null);
先不論查得出查不出,但看這樣的寫法就值得推敲!
首先,android給的這個query方法,是為了方便我們查詢使用,其中第二個參數代表列名。你這樣的寫法是否可以理解為列名就為:MAX("+GAME_COLUMN+") AS GC,而你創建表的時候,列名可只有GAME_COLUMN,並沒有MAX
如果你想查詢最大值,最好使用sql語句查詢,而非這個函數