在我的应用程序中,有几行代码用于从多个数据库中检索数据。
代码:
String testquiry = "select a.rowid as _id, "
+ "a.delivery_seq sequence, "
+ "a.chinese_name chinese_name, "
+ "b.cust_code cust_code, "
+ "a.status status "
+ "from route_cust a, customer_file b, pda c "
+ "where a.cust_code = b.cust_code "
+ "and b.cust_status = 'A' "
+ "and b.route = c.route "
+ "and c.default_route = 'Y' "
+ "order by a.delivery_seq";
String testResult;
Cursor testCur = db.rawQuery(testquiry, null);
if(testCur.moveToFirst()){
testResult = testCur.getString(cur_getimportcsv.getColumnIndex("status"));
Toast.makeText(v.getContext(), testResult, Toast.LENGTH_LONG).show();
}
testCur.close();
当应用程序运行到以下代码时,会发生错误。
testResult = testCur.getString(cur_getimportcsv.getColumnIndex("status"));
LogCat中显示错误:
09-03 15:53:36.533: W/dalvikvm(23398): threadid=1: thread exiting with uncaught exception (group=0x415abba8)
09-03 15:53:36.563: E/AndroidRuntime(23398): FATAL EXCEPTION: main
09-03 15:53:36.563: E/AndroidRuntime(23398): Process: com.iceapp, PID: 23398
09-03 15:53:36.563: E/AndroidRuntime(23398): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
09-03 15:53:36.563: E/AndroidRuntime(23398): at android.database.CursorWindow.nativeGetString(Native Method)
09-03 15:53:36.563: E/AndroidRuntime(23398): at android.database.CursorWindow.getString(CursorWindow.java:434)
09-03 15:53:36.563: E/AndroidRuntime(23398): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
09-03 15:53:36.563: E/AndroidRuntime(23398): at com.iceapp.ImportCsvActivity$2.onClick(ImportCsvActivity.java:285)
09-03 15:53:36.563: E/AndroidRuntime(23398): at android.view.View.performClick(View.java:4438)
09-03 15:53:36.563: E/AndroidRuntime(23398): at android.view.View$PerformClick.run(View.java:18422)
09-03 15:53:36.563: E/AndroidRuntime(23398): at android.os.Handler.handleCallback(Handler.java:733)
09-03 15:53:36.563: E/AndroidRuntime(23398): at android.os.Handler.dispatchMessage(Handler.java:95)
09-03 15:53:36.563: E/AndroidRuntime(23398): at android.os.Looper.loop(Looper.java:136)
09-03 15:53:36.563: E/AndroidRuntime(23398): at android.app.ActivityThread.main(ActivityThread.java:5001)
09-03 15:53:36.563: E/AndroidRuntime(23398): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 15:53:36.563: E/AndroidRuntime(23398): at java.lang.reflect.Method.invoke(Method.java:515)
09-03 15:53:36.563: E/AndroidRuntime(23398): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-03 15:53:36.563: E/AndroidRuntime(23398): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-03 15:53:36.563: E/AndroidRuntime(23398): at dalvik.system.NativeStart.main(Native Method)
我在stackoverflow上研究了其他类似的问题,我发现这个问题是由于获取列" status"失败造成的。谁能给我一些帮助?为什么' status'的价值?不能成功吗?
答案 0 :(得分:1)
您使用不同的光标(cur_getimportcsv
)作为查找列" status"的参考。它可能存在,也可能不存在。在这种情况下,它不是:没有"状态"在cur_getimportcsv
上,返回" -1"这个职位。
操作光标时使用不同的光标几乎从不安全。解决方案是一直使用相同的光标。
testResult = testCur.getString(testCur.getColumnIndex("status"));
答案 1 :(得分:0)
如果您只想阅读第一行,可以
testCur.moveToFirst();
if (!testCur.isAfterLast()) {
testResult = testCur.getString(testCur.getColumnIndex("status"));
}
您可以按如下方式遍历光标
testCur.moveToFirst();
while (testCur.isAfterLast() == false)
{
testResult = testCur.getString(testCur.getColumnIndex("status"));
// implement your logic here
cur.moveToNext();
}