android.database.sqlite.SQLiteException:绑定或列索引超出范围

时间:2011-11-03 10:48:21

标签: android database sqlite

我正在从数据库中的服务器存储一些值,但是当我将值插入数据库时​​它给了我以下异常:

   android.database.sqlite.SQLiteException: bind or column index out of range

我正在使用以下代码:

       private static final String INSERT = "insert into " 
         + DATABASE_TABLE + "(type,width,height,encoding,data,iid)"+" values (?,?,?,?,?,?)";


     public WineDatabaseAdapter(Context context) {
          this.context = context;
       WineDatabaseHelper openHelper = new WineDatabaseHelper(context);
       this.db=openHelper.getWritableDatabase();
      this.insertStmt=this.db.compileStatement(INSERT);
       }


public long insert(String KEY_TYPE ,String KEY_WIDTH,String KEY_HEIGHT, String KEY_ENCODING,String KEY_DATA,String KeyIId){
           this.insertStmt.bindString(0, KEY_TYPE);
           this.insertStmt.bindString(1, KEY_WIDTH);
           this.insertStmt.bindString(2, KEY_HEIGHT);
           this.insertStmt.bindString(3, KEY_ENCODING);
           this.insertStmt.bindString(4, KEY_DATA);
           this.insertStmt.bindString(5, KeyIId);
           return this.insertStmt.executeInsert();
       }

我正在其他类中插入值:

    db = new WineDatabaseAdapter(HomePageWithPhoneIsOfflineDialog.this);
  db.insert( type,width, height,encoding, data1,iid);

以下是stacktrace:

         11-03 17:32:15.406: WARN/System.err(22895): android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x319db0
        11-03 17:32:15.406: WARN/System.err(22895):     at android.database.sqlite.SQLiteProgram.native_bind_string(Native Method)
        11-03 17:32:15.406: WARN/System.err(22895):     at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:244)
        11-03 17:32:15.406: WARN/System.err(22895):     at com.emx.Winwcountry.database.WineDatabaseAdapter.insert(WineDatabaseAdapter.java:52)
        11-03 17:32:15.406: WARN/System.err(22895):     at com.emx.Winwcountry.HomePageWithPhoneIsOfflineDialog$1.onClick(HomePageWithPhoneIsOfflineDialog.java:144)
       11-03 17:32:15.406: WARN/System.err(22895):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
         11-03 17:32:15.406: WARN/System.err(22895):     at android.os.Handler.dispatchMessage(Handler.java:99)
        11-03 17:32:15.406: WARN/System.err(22895):     at android.os.Looper.loop(Looper.java:130)
      11-03 17:32:15.406: WARN/System.err(22895):     at android.app.ActivityThread.main(ActivityThread.java:3683)
         11-03 17:32:15.406: WARN/System.err(22895):     at java.lang.reflect.Method.invokeNative(Native Method)
       11-03 17:32:15.406: WARN/System.err(22895):     at java.lang.reflect.Method.invoke(Method.java:507)
       11-03 17:32:15.406: WARN/System.err(22895):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
          11-03 17:32:15.406: WARN/System.err(22895):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
         11-03 17:32:15.406: WARN/System.err(22895):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:7)

我认为这很简单。只需阅读错误。

  
    

绑定或列索引超出范围

  
你有6个吗?在你的字符串中:

 private static final String INSERT = "insert into " 
     + DATABASE_TABLE + "(type,width,height,encoding,data,iid)"+" values (?,?,?,?,?,?)";

并且您正尝试在此处绑定索引为7的列:

this.insertStmt.bindString(7, KeyIId);

修改 bindString使用的索引是从1开始的,因此您必须从1开始索引。

答案 1 :(得分:0)

看你已经提到了6列,但是你在insert()函数中设置了7列。还要从0开始索引而不是1.因此你的索引范围是0到5。