Reputation: 508
I have created a table like Players
in onCreate() method which is in MySQLiteHelper.java:
String CREATE_PLAYERS_TABLE = "CREATE TABLE Players
(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, password TEXT, score INTEGER)";
db.execSQL(CREATE_PLAYERS_TABLE);
And I have an update method in the same class like:
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;
}
and when I call functions to change score
variable as:
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));
}
I am getting a logcat error like:
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.
Thanks.
There has been another query that causes this specific exception and I found out.
Upvotes: 2
Views: 2962
Reputation: 152817
The exception comes from accessing cursor columns and the code you posted doesn't show that.
However, column indexing starts from 0, so to access the first column value as string, use getString(0)
instead of getString(1)
.
Upvotes: 5