我有一个数据库叫做test_数据库。
我有一张桌子叫tbl_marks
。
在tbl_marks
中有以下行:
_身份证
名称
子ID
格威治
资料
共有4个条目。两个带子id 1,另一个带子id 2。
现在我要做的是:
SELECT * FROM tbl_marks WHERE subid = 1;
最后,我喜欢在列表视图中列出所有
subid
1、2、3或其他标记。我怎么能用光标来做呢?
我试着通过一个教程来学习,但是失败了…
Higherpass Android Tutorial Accessing-Data-With-Android-Cursors
Anddev Working with the sqlite database - Cursors
代码:注释部分是我之前得到的(它列出了所有内容)
private void fillData() {
// Get all of the notes from the database and create the item list
mDbHelper.open_database_rw();
Cursor mCursor = db.rawQuery("SELECT _id, subid, name, mark, gewicht, datum FROM tbl_marks ORDER BY subid;", null);
startManagingCursor(mCursor);
if (mCursor != null) {
int intName = mCursor.getColumnIndexOrThrow("subid");
do {
teststring = mCursor.getString(intName);
String[] from = new String[] { dbHelper.KEY_NAME_MARKS, dbHelper.KEY_MARK_MARKS, dbHelper.KEY_GEWICHT_MARKS, dbHelper.KEY_DATUM_MARKS};
int[] to = new int[] {R.id.txt_marks_row, R.id.txt_note, R.id.txt_gewicht, R.id.txt_datum};
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.show_marks, mCursor, from, to);
setListAdapter(notes);
} while (mCursor.moveToNext());
}
LogCat说,柱子ID不存在。但它在那里,数据库已经打开了。
02-07 11:30:00.500: D/dalvikvm(9913): GC_EXTERNAL_ALLOC freed 54K, 50% free 2694K/5379K, external 0K/0K, paused 56ms
02-07 11:30:00.535: D/dalvikvm(9913): GC_EXTERNAL_ALLOC freed 4K, 50% free 2698K/5379K, external 13K/523K, paused 27ms
02-07 11:30:10.457: D/AndroidRuntime(9913): Shutting down VM
02-07 11:30:10.457: W/dalvikvm(9913): threadid=1: thread exiting with uncaught exception (group=0x40235568)
02-07 11:30:10.457: E/AndroidRuntime(9913): FATAL EXCEPTION: main
02-07 11:30:10.457: E/AndroidRuntime(9913): java.lang.RuntimeException: Unable to start activity ComponentInfo{test.marco.notenha/test.marco.notenha.mark}: java.lang.IllegalArgumentException: column 'subid' does not exist
02-07 11:30:10.457: E/AndroidRuntime(9913): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1664)
02-07 11:30:10.457: E/AndroidRuntime(9913): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
02-07 11:30:10.457: E/AndroidRuntime(9913): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-07 11:30:10.457: E/AndroidRuntime(9913): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-07 11:30:10.457: E/AndroidRuntime(9913): at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 11:30:10.457: E/AndroidRuntime(9913): at android.os.Looper.loop(Looper.java:130)
02-07 11:30:10.457: E/AndroidRuntime(9913): at android.app.ActivityThread.main(ActivityThread.java:3703)
02-07 11:30:10.457: E/AndroidRuntime(9913): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 11:30:10.457: E/AndroidRuntime(9913): at java.lang.reflect.Method.invoke(Method.java:507)
02-07 11:30:10.457: E/AndroidRuntime(9913): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-07 11:30:10.457: E/AndroidRuntime(9913): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-07 11:30:10.457: E/AndroidRuntime(9913): at dalvik.system.NativeStart.main(Native Method)
02-07 11:30:10.457: E/AndroidRuntime(9913): Caused by: java.lang.IllegalArgumentException: column 'subid' does not exist
02-07 11:30:10.457: E/AndroidRuntime(9913): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
02-07 11:30:10.457: E/AndroidRuntime(9913): at test.marco.notenha.mark.fillData(mark.java:180)
02-07 11:30:10.457: E/AndroidRuntime(9913): at test.marco.notenha.mark.onCreate(mark.java:70)
02-07 11:30:10.457: E/AndroidRuntime(9913): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-07 11:30:10.457: E/AndroidRuntime(9913): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1628)
02-07 11:30:10.457: E/AndroidRuntime(9913): ... 11 more
这是第180行:int intname=mcursor.getColumnIndexOrthRow(“subid”);
02-07 11:52:26.304: D/dalvikvm(10075): GC_EXTERNAL_ALLOC freed 48K, 50% free 2694K/5379K, external 0K/0K, paused 38ms
02-07 11:52:26.339: D/dalvikvm(10075): GC_EXTERNAL_ALLOC freed 4K, 50% free 2698K/5379K, external 13K/523K, paused 30ms
02-07 11:52:29.433: D/AndroidRuntime(10075): Shutting down VM
02-07 11:52:29.433: W/dalvikvm(10075): threadid=1: thread exiting with uncaught exception (group=0x40235568)
02-07 11:52:29.437: E/AndroidRuntime(10075): FATAL EXCEPTION: main
02-07 11:52:29.437: E/AndroidRuntime(10075): java.lang.RuntimeException: Unable to start activity ComponentInfo{test.marco.notenha/test.marco.notenha.mark}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 3
02-07 11:52:29.437: E/AndroidRuntime(10075): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1664)
02-07 11:52:29.437: E/AndroidRuntime(10075): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
02-07 11:52:29.437: E/AndroidRuntime(10075): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-07 11:52:29.437: E/AndroidRuntime(10075): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-07 11:52:29.437: E/AndroidRuntime(10075): at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 11:52:29.437: E/AndroidRuntime(10075): at android.os.Looper.loop(Looper.java:130)
02-07 11:52:29.437: E/AndroidRuntime(10075): at android.app.ActivityThread.main(ActivityThread.java:3703)
02-07 11:52:29.437: E/AndroidRuntime(10075): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 11:52:29.437: E/AndroidRuntime(10075): at java.lang.reflect.Method.invoke(Method.java:507)
02-07 11:52:29.437: E/AndroidRuntime(10075): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-07 11:52:29.437: E/AndroidRuntime(10075): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-07 11:52:29.437: E/AndroidRuntime(10075): at dalvik.system.NativeStart.main(Native Method)
02-07 11:52:29.437: E/AndroidRuntime(10075): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 3
02-07 11:52:29.437: E/AndroidRuntime(10075): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
02-07 11:52:29.437: E/AndroidRuntime(10075): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
02-07 11:52:29.437: E/AndroidRuntime(10075): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
02-07 11:52:29.437: E/AndroidRuntime(10075): at test.marco.notenha.mark.fillData(mark.java:183)
02-07 11:52:29.437: E/AndroidRuntime(10075): at test.marco.notenha.mark.onCreate(mark.java:70)
02-07 11:52:29.437: E/AndroidRuntime(10075): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-07 11:52:29.437: E/AndroidRuntime(10075): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1628)
02-07 11:52:29.437: E/AndroidRuntime(10075): ... 11 more
最佳答案
“subid”列必须在“选择列”列表中,以便mcursor.getColumnIndexOrthRow(“subid”)调用返回它:
SELECT _id, name, mark, gewicht, datum, subid FROM tbl_marks ORDER BY subid;
编辑:
现在需要调用mcursor.moveToFirst()并检查它是否返回“true”(即结果列表不是空的)。见this question。例如:
if (mCursor != null && mCursor.moveToFirst()) {
int intName = mCursor.getColumnIndexOrThrow("subid");
do {
teststring = mCursor.getString(intName);
...
} while (mCursor.moveToNext());
}
关于android - 如何从SQLite表读取一行的多个记录?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9174230/