SQLite不能识别插入

想要在表单中新加入一行然后确认是否加入正确,等到查询的时候,程序就崩溃了。

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语句查询,而非这个函数

if(cursor2.getcount>0)
{
cursor2.moveTofirst();
String temp = cursor2.getString(cursor.getColumnIndex("GC"));
}

加入我的代码就可以了

判断你的cursor是否为空,是否有数据,上面Kill_it的代码是建立在cursor有效的情况下的,所以你还是要判断下的。你的代码里curosr还是在初始化的位置 -1,它不知道要找什么。