Вы пытаетесь преобразовать значение SELECT ......... в число согласно int win = Integer.parseInt(query);
.
Для оценки SELECT вам нужно получить курсор (набор результатов) с помощью запроса или rawQuery метода SQLiteDatabase, а затем извлечь значения из объекта метод и затем получить доступ к соответствующему столбцу из соответствующих строк.
Я считаю, что вы бы использовали что-то вроде: -
public int getWin(String id){
SQLiteDatabase db = this.getWritableDatabase();
int rv = -1; //<<<<<<<<<< ADDED default value to return if no row found
String query = "SELECT " + COL3 + " FROM " + TABLE_NAME +
" WHERE " + COL2 + " = '" + id + "'";
Log.d(TAG, "updateName: query: " + query);
Cursor csr = db.rawQuery(query,null); //<<<<<<<<<< CHANGED to get the Cursor returned
// ADDED the following IF construct
if (csr.moveToFirst()) {
rv = csr.getInt(csr.getColumnIndex(COL3));
}
//int win = Integer.parseInt(query); //<<<<<<<<<< DELETED (commented out)
csr.close(); //<<<<<<<<<< ADDED should always close a Cursor when done with it
return rv; //<<<<<<<<<< return the value (-1 if no row found)
}
- Предполагается, что вы просто хотите получить значение из одной строки, как указано в предложении WHERE.
Если возможно, рекомендуется а) не создавать запрос с прямыми значениями (делает его уязвимым для SQL-инъекций) и б) использовать удобный запрос метод.
Примените оба a и b , и ваш код может быть: -
public int getWin(String id){
SQLiteDatabase db = this.getWritableDatabase();
int rv = -1;
String whereclause = COL2 + "=?"; //<<<<<<<<<< where clause without where and ? for value that will be passed
String[] whereargs = new String[]{String.valueOf(id)}; //<<<<<<<<<< arguments used by the whereclause ? replaced on a 1 for 1 basis
String[] columns = new String[]{COL3}; //<<<<<<<<<< the columns to extract as a String array
Cursor csr = db.query(TABLE_NAME,columns,whereclause,whereargs,null,null,null);
if (csr.moveToFirst()) {
rv = csr.getInt(csr.getColumnIndex(COL3));
}
csr.close();
return rv;
}