I have a Database which extends SQLiteOnHelper for an app of Then I have to make sure that if, for example, I change only the title, you should update the record, otherwise normal insert.
This is my table:
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE vasca(" +
"_id INTEGER PRIMARY KEY,"+
"titoloNota TEXT NOT NULL,"+
"testoNota TEXT NOT NULL,"+
"dataNota DATE,"+
"coloreNota INTEGER NOT NULL,"+
"UNIQUE(id, titoloNota)"+ // to be reviewed
")";
db.execSQL(sql);
}
And my boolean method for insert:
public boolean insertNote(Nota nota){
// boolean inizializzat a false utilizzata come return
boolean resultInsert = false;
// repository dei dati in modalità scrittura
SQLiteDatabase dbLite = this.getWritableDatabase();
// utilizza un ContentValues come mappa di valori, dove le columns rappresentano le chiavi
ContentValues values = new ContentValues();
values.put(SQL_INSERT_OR_REPLACE, true);
values.put("titoloNota", nota.getTitolo());
values.put("testoNota", nota.getTesto());
values.put("dataNota", nota.getData()); // ritorna una string sdf.format(nota.getData())
values.put("coloreNota", nota.getColore());
// chiama il metodo insert su dbLite, Vasca è il nome della tabella
// poichè ritorna un long, se negativo significa che l'inserimento non è riuscito
long idInserimento = dbLite.insert("vasca", null, values);
// sfrutto la variabile long per fare un controllo se andato tutto ok
if( idInserimento <= -1 ){
Log.e(TAG, "Inserimento della nota non avvento");
resultInsert = false;
}else{
resultInsert = true;
// inserisce la nota passata al metodo nell'arrayList listNote chiamando il metodo add
}
dbLite.close();
return resultInsert;
}
I think we need to make a query. any help or advice is appreciated :)