1

I am storing some values that I am getting from server in the database but when i insert values into database It gives me following exception:

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

I am using the following code for it:

       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();
       }

and I am inserting values in other class :

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

and following is the 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 Answers 2

7

I think it is pretty straight forward. Just read the error.

bind or column index out of range

You have 6 ? in your string :

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

and you are trying to bind a column with index 7 here :

this.insertStmt.bindString(7, KeyIId);

Edit: The index used by bindString is 1-based, so you have to start your index from 1.

Sign up to request clarification or add additional context in comments.

7 Comments

I have corrected that also instead it is giving the same exception
can you post the code where you have initialised insertStmt
Your code looks fine. Could you post the stacktrace. Maybe we could find something there.
Please make sure that the type,width,height,encoding,data,iid columns are present in the table you are querying.
Also make sure that you are not passing any null values when you are binding the parameters.
|
0

see you have mentioned 6 columns but you are setting 7 columns inside your insert() function.Also start the index from 0 not from 1.So your index will range from 0 to 5.

2 Comments

I changed the code acc to you but it is still giving same exception
@ekjyot.All datatypes correct according to table's column datatype?.Please check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.