我在Android应用程序上工作,我想从Sqlite数据库表中获取数据。
即使表格中存在数据,也会给我错误。
代码:
public List<Mikats> getAllMikats() {
List<Mikats> contactList = new ArrayList<Mikats>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME_2;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
//cursor.moveToFirst()
int rows=cursor.getCount();
if (rows>0) {
do {
Mikats mikat = new Mikats();
int mid=cursor.getInt(1);
String sqlectEventTitle="select * from "+ TABLE_NAME_1 +" where "+ KEY_MID+" = "+mid;
SQLiteDatabase db2 = this.getWritableDatabase();
Cursor cursor1=db2.rawQuery(sqlectEventTitle, null);
if(cursor1.moveToNext())
{
String Title=cursor1.getString(cursor1.getColumnIndex("EventTitle"));
mikat.setEventTitle(Title);
}
mikat.setEjamatID(Integer.parseInt(cursor.getString(0)));
mikat.setMikatID(Integer.parseInt(cursor.getString(1)));
mikat.setStartDate(cursor.getString(2));
mikat.setEndDate(cursor.getString(3));
mikat.setBlock(cursor.getString(4));
mikat.setFloor(cursor.getString(5));
mikat.setGate(cursor.getString(6));
String masjid=cursor.getString(7);
mikat.setMasjid(masjid);
mikat.setAutoID(Integer.parseInt(cursor.getString(8)));
mikat.setSeatNo(cursor.getString(9));
// Adding contact to list
contactList.add(mikat);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
错误行:
int mid=cursor.getInt(1);
logcat的:
09-17 11:51:57.904: W/System.err(19441): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
09-17 11:51:57.904: W/System.err(19441): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
09-17 11:51:57.904: W/System.err(19441): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
09-17 11:51:57.914: W/System.err(19441): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
09-17 11:51:57.924: W/System.err(19441): at com.example.busahebaapp.MikatHandler.getAllMikats(MikatHandler.java:113)
09-17 11:51:57.924: W/System.err(19441): at com.example.busahebaapp.ViewEvents.onCreate(ViewEvents.java:57)
09-17 11:51:57.934: W/System.err(19441): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-17 11:51:57.944: W/System.err(19441): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-17 11:51:57.944: W/System.err(19441): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-17 11:51:57.944: W/System.err(19441): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-17 11:51:57.954: W/System.err(19441): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-17 11:51:57.964: W/System.err(19441): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 11:51:57.974: W/System.err(19441): at android.os.Looper.loop(Looper.java:123)
09-17 11:51:57.974: W/System.err(19441): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-17 11:51:57.984: W/System.err(19441): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 11:51:57.994: W/System.err(19441): at java.lang.reflect.Method.invoke(Method.java:507)
09-17 11:51:57.994: W/System.err(19441): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-17 11:51:58.004: W/System.err(19441): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-17 11:51:58.004: W/System.err(19441): at dalvik.system.NativeStart.main(Native Method)
09-17 11:53:24.805: W/jdwp(19441): Debugger is telling the VM to exit with code=1
请帮助我,我不理解错误的原因。
我确保数据存在于表格中。
答案 0 :(得分:2)
只需改变:
if (rows>0) {
cursor.moveToFirst();
do{
// get data here from database...
}while(cursor.moveToNext());
}
当第一次光标移动到第一个位置时,当它不为空且其计数必须为> 0 ..并且该值必须以0开始而不是从1开始...所以更改
int mid=cursor.getInt(0);
答案 1 :(得分:1)
数据库中只有一个值,因此将其读为0索引而不是1,游标从0开始。 使用cursor.moveTofirst();
int mid=cursor.getInt(0);