我正在使用android和sqlite数据库,我想从列nomMission
获取数据
这就是我所做的,但它不起作用
public List<Missions> getMissions() {
List<Missions> mission = new ArrayList<Missions>();
Cursor cursor = mDb.rawQuery("select nomMission from Missions", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Missions missionss = cursorToMission(cursor);
mission.add(missionss);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return mission;
}
private Missions cursorToMission(Cursor cursor) {
Missions missions = new Missions(null, null);
missions.setNomMission(cursor.getString(1));
missions.setDateMission(cursor.getString(2));
return missions;
}
错误日志:
04-29 16:04:52.347: E/AndroidRuntime(4864): FATAL EXCEPTION: main
04-29 16:04:52.347: E/AndroidRuntime(4864): java.lang.RuntimeException: Unable to start activity ComponentInfo{tn.pfe.ybn.sigl/tn.pfe.ybn.sigl.DataManip.MissionAct}: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
04-29 16:04:52.347: E/AndroidRuntime(4864): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
04-29 16:04:52.347: E/AndroidRuntime(4864): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
04-29 16:04:52.347: E/AndroidRuntime(4864): at android.app.ActivityThread.access$600(ActivityThread.java:134)
04-29 16:04:52.347: E/AndroidRuntime(4864): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
04-29 16:04:52.347: E/AndroidRuntime(4864): at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 16:04:52.347: E/AndroidRuntime(4864): at android.os.Looper.loop(Looper.java:154)
04-29 16:04:52.347: E/AndroidRuntime(4864): at android.app.ActivityThread.main(ActivityThread.java:4624)
04-29 16:04:52.347: E/AndroidRuntime(4864): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 16:04:52.347: E/AndroidRuntime(4864): at java.lang.reflect.Method.invoke(Method.java:511)
04-29 16:04:52.347: E/AndroidRuntime(4864): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
04-29 16:04:52.347: E/AndroidRuntime(4864): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
04-29 16:04:52.347: E/AndroidRuntime(4864): at dalvik.system.NativeStart.main(Native Method)
04-29 16:04:52.347: E/AndroidRuntime(4864): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
答案 0 :(得分:1)
因为光标实际上只包含一列 哪个索引 0 (表格列基于0)。
您指定:
Cursor cursor = mDb.rawQuery("select nomMission from Missions", null);
(一栏)
但是你试着访问
missions.setNomMission(cursor.getString(1));
missions.setDateMission(cursor.getString(2));
在您的情况下,您只能访问:
missions.setNomMission(cursor.getString(0));
或者更好,
missions.setNomMission(cursor.getString(cursor.getColumnIndex("nomMission")));
您可以通过名称引用列,而不是索引
答案 1 :(得分:0)
如果您的查询只选择一列,它只会返回一个索引为0的列。现在的方式,您可以调用cursor.getString(0)
,而且确实如此。如果你想获得NomMission和DateMission,你应该致电
`Cursor cursor = mDB.rawQuery(&#34; SELECT nomMission AND dateMission FROM Missions&#34;,null);
就个人而言,我更喜欢使用SQLiteDatabase&#39; query method here。
对你而言,这看起来像是
Cursor cursor = mDb.query("Missions, new String[]{"nomMission", "dateMission"},null, null, null, null, null, null);
我也是一个菜鸟,所以任何人都可以随意纠正任何事情。