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
=======================================================================