Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before

在做项目时碰到一个问题,就是一直报 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});




版权声明:本文为博主原创文章,未经博主允许不得转载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值