在做项目时碰到一个问题,就是一直报 java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:434)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at com.handaer.db.dao.DataQuery.getOrder(DataQuery.java:51)
at org.winplus.serial.test.MethodTest.testGetOrder(MethodTest.java:29)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
后来追踪到 orderString=cursor.getString(cursor.getColumnIndex(key)); 有问题,把key直接改为数据库中的字段名称还是报错,最后原来是所有查询的字段不能用"?",改为下面这句就好了,在此做下记录。
Cursor cursor=db.rawQuery("select "+key+" from data where name=? and type=? and brand=? and series=?", new String[]{name,type,brand,series});
key 是传过来的参数
Cursor cursor=db.rawQuery("select ? from data where name=? and type=? and brand=? and series=?", new String[]{key,name,type,brand,series});if(cursor.moveToNext()){
System.out.println("cursor大小:"+cursor.getCount());
System.out.println("key>>"+key);
orderString=cursor.getString(cursor.getColumnIndex(key));
System.out.println("orderString:"+orderString);
}
相应地,在插入数据时也报错了
db.execSQL("insert into data(name,type,brand,series,?) values(?,?,?,?,?)", new String[]{key,name,type,brand,series,order});
改为如下就好了:
db.execSQL("insert into data(name,type,brand,series,"+key+") values(?,?,?,?,?)", new String[]{name,type,brand,series,order});
版权声明:本文为博主原创文章,未经博主允许不得转载。