2

Android SQLLite gives me the following error:

 SQLiteStatement tgStatement = dbo.compileStatement("INSERT OR REPLACE INTO Game_Team" +
                "(teamId, gameId, pos, score) VALUES (?,?,?,?) ");

Thats why I try to do:

tgStatement.bindLong(0, 1);

I get an error

java.lang.IllegalArgumentException: Cannot bind argument at index 0 because the index is out of range.

The statement has 4 parameters.

So, my question is, if there are 4 parameters, why is it complaining about argument at index 0? how is that out of range?

3 Answers 3

8

The parameters are 1-indexed. The index needs to run from 1 to 4, so 0 is out of range.

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

1 Comment

Oh wow. just when I didn't expect. I spent countless hours trying to figure this out. Searching left and right, and then I finally broke... thank you very much!
2

This trick might come handy to some.

SQLiteStatement tgStatement = dbo.compileStatement("INSERT OR REPLACE INTO Game_Team" +
                "(teamId, gameId, pos, score) VALUES (?,?,?,?) ");
    int i = 1;
    tgStatement.bindLong(i++, teamId);
    tgStatement.bindLong(i++, gameId);
    tgStatement.bindString(i++, pos);
    tgStatement.bindLong(i++, score);

Above trick is useful when you have more parameters, it avoid missing on indices as well as numbering them. i variable handles the indice.

Comments

1

Indices start at 1. So it's setLong(1,1)

Comments

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.