gpt4 book ai didi

android - 无法从 CursorWindow (Android) 读取第 0 行、第 1 列

转载 作者:太空狗 更新时间:2023-10-29 16:38:09 25 4
gpt4 key购买 nike

我在 MySQLiteHelper.java 中的 onCreate() 方法中创建了一个类似 Players 的表:

String CREATE_PLAYERS_TABLE = "CREATE TABLE Players 
(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, password TEXT, score INTEGER)";

db.execSQL(CREATE_PLAYERS_TABLE);

我在同一个类中有一个更新方法,例如:

public int PlayerUpdate(Player PL) {

// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();

// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put("name", PL.getName());
values.put("password", PL.getPassword());
values.put("score", PL.getScore());

// 3. updating row
int i = db.update(TABLE_Players, // table
values, // column/value
KEY_ID + " = ?", // selections
new String[] { String.valueOf(PL.getId()) }); // selection args

// 4. close
db.close();

return i;
}

当我调用函数将 score 变量更改为:

public void scoreUp() {
helper.PlayerUpdate(new Player(player_name,player_password,player_score+5));
}
public void scoreDown() {
helper.PlayerUpdate(new Player(player_name,player_password,player_score-5));
}

我收到如下 logcat 错误:

Failed to read row 0, column 1 from a CursorWindow which has 1 rows, 1 columns.
FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.
Make sure the Cursor is initialized correctly before accessing data from it.

谢谢。

已解决:

还有另一个查询导致了这个特定的异常,我发现了。

最佳答案

异常来自访问游标列,您发布的代码没有显示。

但是,列索引从 0 开始,因此要将第一列值作为字符串访问,请使用 getString(0) 而不是 getString(1)

关于android - 无法从 CursorWindow (Android) 读取第 0 行、第 1 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23202249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com