I tried to load a list of values from a table, but the cursor doesn’t find the columns?
The Code which is buggy:
public final AccessToken[] fetchAccessTokenByServerId(final long serverId) {
final Cursor c = db.query(
ACCESS_TOKEN_TABLE,
new String[] { ACCESS_TOKEN_COL_ID, ACCESS_TOKEN_COL_VALUE },
ACCESS_TOKEN_COL_SERVER_ID + "=?",
new String[] { Long.toString(serverId) },
null,
null,
null);
AccessToken[] result = new AccessToken[c.getCount()];
for (int i = 0; i < result.length; i++) {
long id = c.getLong(c.getColumnIndexOrThrow(ACCESS_TOKEN_COL_ID));
String value = c.getString(c.getColumnIndexOrThrow(ACCESS_TOKEN_COL_VALUE));
result[i] = new AccessToken(value, id, serverId);
c.moveToNext();
}
return result;
}
public static final String COL_ID = "_id";
public static final String ACCESS_TOKEN_TABLE = "accesstoken";
public static final String ACCESS_TOKEN_COL_ID = COL_ID;
public static final String ACCESS_TOKEN_COL_SERVER_ID = "server_id";
public static final String ACCESS_TOKEN_COL_VALUE = "value";
But the first two statements in the for-loop fails:
Index -1 is requested, with a size of
1
The table exists with the columns: _id (= int), server_id (= int) and value (= string)
There is one entry: 1, 1, test
The parameter of the function is 1.
Sincerely
xZise
Just after initializing your cursor, do this: