2012-12-07 76 views
0

任何人都可以看到为什么这不起作用。我试图向表中添加一个简单的行,然后确认它已正确添加。当我试图查询表时,我的程序崩溃。我的头因久久盯着这个而感到痛苦。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); 
} 

螺纹与未捕获的异常

同样离开,这里是从表中创建其中代码:

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); 

回答

1

就没有检查this.that光标会空值? 只是试试这个代码。

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

试试这是它的工作。

+0

工作正常!我不明白为什么当我插入记录时我不得不检查空游标,但在这一点上我不在乎。如果我能给你1000000 + 1的话,我会的。非常感谢! – Alex

+0

大多数WelCome亲爱的。 –