Witam ponownie na swoim ulubionym forum.
Mam nadzieję że już niedługo będę nie tylko pytał ale też i pomogał bo wkręcam się już w to coraz bardziej 🙂.
Dziś mam nieco inny temat do rozwiązania. Mam na niego koncepcję ale chyba coś robię źle bo niestety nie działa. Chciałbym po naciśnięciu przycisku wykonać na bazie insert lub update w zależności czy dany wiersz istnieje czy nie. W tym celu stworzyłem sobe boolowską metodę select do której jako parametr wrzucam id potrzebnego do klauzuli WHERE. Tak to wygląda:
public boolean select (int id) {
SQLiteDatabase db = this.getReadableDatabase();
String sqlSelect = "SELECT * FROM " + TABLE_NAME + " WHERE " + COL_2 + " = " + id;
Cursor cursor = db.rawQuery(sqlSelect, null);
if(cursor != null) {
cursor.moveToFirst();
return true;
}
else {
return false;
}
}
Metoda zwracam mi true jeśli wiersz po podanym id zostanie znaleziony, lub false jeśli go nie znajdzie. Następnie mam jeszcze dwie metody które robię mi update i insert, tak one wyglądają:
public boolean update (Integer round, String player1, String total1_after, String total1_before, String score1, String coins1, Boolean queen1, Boolean break1,
String player2, String total2_after, String total2_before, String score2, String coins2, Boolean queen2, Boolean break2) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, round);
contentValues.put(COL_3, player1);
contentValues.put(COL_4, total1_after);
contentValues.put(COL_5, total1_before);
contentValues.put(COL_6, score1);
contentValues.put(COL_7, coins1);
contentValues.put(COL_8, queen1);
contentValues.put(COL_9, break1);
contentValues.put(COL_10, player2);
contentValues.put(COL_11, total2_after);
contentValues.put(COL_12, total2_before);
contentValues.put(COL_13, score2);
contentValues.put(COL_14, coins2);
contentValues.put(COL_15, queen2);
contentValues.put(COL_16, break2);
long result = db.update(TABLE_NAME, contentValues, " WHERE " + COL_2 + " = " + round, null);
if (result == -1) {
return false;
}
else {
return true;
}
}
public boolean insertData(Integer round, String player1, String total1_after, String total1_before, String score1, String coins1, Boolean queen1, Boolean break1,
String player2, String total2_after, String total2_before, String score2, String coins2, Boolean queen2, Boolean break2) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, round);
contentValues.put(COL_3, player1);
contentValues.put(COL_4, total1_after);
contentValues.put(COL_5, total1_before);
contentValues.put(COL_6, score1);
contentValues.put(COL_7, coins1);
contentValues.put(COL_8, queen1);
contentValues.put(COL_9, break1);
contentValues.put(COL_10, player2);
contentValues.put(COL_11, total2_after);
contentValues.put(COL_12, total2_before);
contentValues.put(COL_13, score2);
contentValues.put(COL_14, coins2);
contentValues.put(COL_15, queen2);
contentValues.put(COL_16, break2);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
}
else {
return true;
}
}
I teraz w klasie głównej robię następującą rzecz, wywołuję metodę select i jeśli zwróci mi ona wartość true (z powinna mi ją zwracać gdy znajdzie wiersz o podanym id) to wykonuję metodę update, jeśli select swróci mi false, wykonuję metodę insert. Wygląda to tak:
boolean select = matchDb.select(count_round);
if (select == true) {
boolean update = matchDb.update(
count_round,
player1.getText().toString(),
total_p1_after.getText().toString(),
total_p1_before,
score_p1.toString(),
coins_p1.getText().toString(),
queen1.booleanValue(),
break1.booleanValue(),
player2.getText().toString(),
total_p2_after.getText().toString(),
total_p2_before.toString(),
score_p2.toString(),
coins_p2.getText().toString(),
queen2.booleanValue(),
break2.booleanValue());
if(update = true) {
Toast.makeText(singles.this, "Data updated", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(singles.this, "Data not updated", Toast.LENGTH_LONG).show();
}
}
else {
boolean isInserted = matchDb.insertData(
count_round,
player1.getText().toString(),
total_p1_after.getText().toString(),
total_p1_before.toString(),
score_p1.toString(),
coins_p1.getText().toString(),
queen1.booleanValue(),
break1.booleanValue(),
player2.getText().toString(),
total_p2_after.getText().toString(),
total_p2_before.toString(),
score_p2.toString(),
coins_p2.getText().toString(),
queen2.booleanValue(),
break2.booleanValue());
if(isInserted = true) {
Toast.makeText(singles.this, "Data inserted", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(singles.this, "Data not inserted", Toast.LENGTH_LONG).show();
}
}
Niestety chyba coś tu jest nie tak bo wywala mi się na metodzie update a robi to dlatego że wykonuje ją pomimo że select nie miał prawa nic zwrócić ponieważ tabela jest pusta. Gdzieś robię błąd ale nie bardzo wiem gdzie. Albo moja logika myślenia jest zła ... sam nie wiem. Staram się próbować ile mogę, zrobić sam ale są sytuacje w któych muszę prosić o pomoc.