Android:Cursor類型的簡單使用

	public void showAllPerson(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		Cursor cursor = db.query("USER", new String[] { "NAME", "PHONE" },
				null, null, null, null, null);
		System.out.println(cursor.getCount());
		while (cursor.moveToNext()) {
			int Nameindex = cursor.getColumnIndex("NAME");
			int Phoneindex = cursor.getColumnIndex("PHONE");
			System.out.println("NAME--->" + cursor.getString(Nameindex) + ","
					+ "PHONE" + cursor.getString(Phoneindex));
			System.out.println("==========================================");
		}

	}

這是我寫的代碼裏面的一個部分方法。用於顯示數據庫裏面的信息的。

查詢的那個表名:USER

此表由兩個列,分別是NAME 和 PHONE 

======================================================================

之前發生的錯誤,是在提取數據的時候,沒有在前面運行 moveToNext() 方法,讓傳說中的下標,下移 ,從而指向第一列。

由於沒有使用那個方法,程序提示錯誤

java.lang.RuntimeException:

 Unable to start activity ComponentInfo{com.zidan/com.zidan.ContactsActivity}: android.database.

CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 

=======================================================================

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章