Cursor c = db.query("user",null,null,null,null,null,null);//查詢並得到遊標 if(c.moveToFirst()){//判斷遊標是否爲空 for(int i=0;i<c.getCount();i++){ c.move(i);//移動到指定記錄 String username = c.getString(c.getColumnIndex("username"); String password = c.getString(c.getColumnIndex("password")); } }
在經過以上代碼讀取sqlite user表中多條數據時,就會發生錯誤:android sqlite android.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5。我把上面代碼改爲如下:android
Cursor c = db.query("user",null,null,null,null,null,null);//查詢並得到遊標 for(int i=0;i<c.getCount() && c!=null ;i++){ if(c.moveToFirst()){//判斷遊標是否爲空 c.move(i);//移動到指定記錄 String username = c.getString(c.getColumnIndex("username"); String password = c.getString(c.getColumnIndex("password")); } }
OK,睡覺了。sql