【问题标题】:SQLite: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parametersSQLite:无法在索引 1 处绑定参数,因为索引超出范围。该语句有 0 个参数
【发布时间】:2013-12-25 23:10:08
【问题描述】:

我收到以下错误,我不知道为什么会发生。我想知道是否有其他人可以阐明这个问题。

12-25 22:52:50.252: E/AndroidRuntime(813): Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range.  The statement has 0 parameters.
12-25 22:52:50.252: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212)
12-25 22:52:50.252: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166)
12-25 22:52:50.252: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
12-25 22:52:50.252: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
12-25 22:52:50.252: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
12-25 22:52:50.252: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
12-25 22:52:50.252: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
12-25 22:52:50.252: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)

代码在这里:

public Player getPlayer(String name) {
    SQLiteDatabase db = this.getReadableDatabase();

    String[] projection = {
            PlayerEntry.COLUMN_NAME_PLAYER_NAME,
            PlayerEntry.COLUMN_NAME_PLAYED_GAMES,
            };

    String selection =  PlayerEntry.COLUMN_NAME_PLAYER_NAME ;
    String[] selectionArgs = new String[1];
    selectionArgs[0] = name;

    Cursor cursor = db.query(
            PlayerEntry.TABLE_NAME,  // The table to query
            projection,                               // The columns to return
            selection,                                // The columns for the WHERE clause
            selectionArgs,                            // The values for the WHERE clause
            null,                                     // don't group the rows
            null,                                     // don't filter by row groups
            null                                 // The sort order
            );

    if (cursor != null)
        cursor.moveToFirst();

【问题讨论】:

  • 从我读到的query 方法看来,selection 似乎不是列名,而是一个完整的 WHERE 子句,可能包含?selectionArgs 的引用。也就是说,selection 应该类似于 "PlayerName=?"selectionArgs 是一个值的列表(如您的代码中所示)。

标签: android sqlite


【解决方案1】:

selection 应该是一个表达式,selectionArgs 应该包含与 selection 中的 ? 文字占位符一样多的元素。

您的selection 不是一个表达式,也没有任何?,但您在selectionArgs 中有一个元素。

你可能想要这样的东西:

String selection =  PlayerEntry.COLUMN_NAME_PLAYER_NAME + "=?";

使其成为匹配玩家姓名列与您在selectionArgs[0]中绑定的文字的表达式。

【讨论】:

  • 就我而言,我在单引号内有问号,例如'?'。删除单引号解决了错误。
  • @mattblang 我认为将您的评论添加到新答案中是个好主意,这样可以帮助更多像我一样遇到此问题的人。想试试吗?
  • @theblang 同样的问题会造成相当长的时间......嘿,如果我们需要在绑定的准备变量周围加上引号怎么办?例如,当你匹配一个字符串时?
【解决方案2】:

在我的情况下,我在单引号内有问号,例如“?”。删除单引号解决了错误。

从上面theblang的评论中复制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 2014-03-29
    相关资源
    最近更新 更多