java.lang.IllegalArgumentException:无法绑定索引0处的参数,因为索引超出范围。该陈述有4个参数

时间:2014-08-31 00:11:47

标签: java android sqlite

Android SQLLite给出了以下错误:

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

这就是我尝试做的原因:

tgStatement.bindLong(0, 1);

我收到错误

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

该陈述有4个参数。

所以,我的问题是,如果有4个参数,为什么它会抱怨索引0处的参数?怎么会超出范围?

3 个答案:

答案 0 :(得分:8)

参数是1索引的。索引需要从1到4运行,因此0超出范围。

答案 1 :(得分:2)

这个技巧对某些人来说可能会派上用场。

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

当您有更多参数时,上面的技巧很有用,它可以避免错过索引以及编号。我变量处理指标。

答案 2 :(得分:1)

指数从1开始。所以它是setLong(1,1)