package com.example.phil.myfirstapp.dao; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import com.example.phil.myfirstapp.metier.Exercice; import java.util.ArrayList; public class ExerciceDAO extends DAOBase { private static final String TABLE_NAME = "Exercices"; private static final String KEY = "idExo"; private static final String NAME = "nom"; private static final String NBREPET = "nbRepetitions"; private static final String WHEN_ = "quand"; public static final String TABLE_CREATE = "create table " + TABLE_NAME + " (" + KEY + " integer primary key autoincrement, " + NAME + " text not null, " + NBREPET + "integer not null check(nbRepetitions > 0), " + WHEN_ + " integer);"; public static final String TABLE_DROP = "drop table if exists " + TABLE_NAME + ";"; public ExerciceDAO(Context context) { super(context); } public void ajouter(Exercice e) { ContentValues value = new ContentValues(); value.put(NAME, e.getNom()); value.put(NBREPET, e.getRepetitions()); value.put(WHEN_, e.getQuand()); db.insert(TABLE_NAME, null, value); } public void supprimer(long id) { db.delete(TABLE_NAME, KEY + " = ?", new String[]{ String.valueOf(id) }); } public void supprimerTout() { db.delete(TABLE_NAME, null,null); } public void modifier(Exercice e) { ContentValues value = new ContentValues(); value.put(NAME,e.getNom()); value.put(NBREPET,e.getRepetitions()); value.put(WHEN_,e.getQuand()); db.update(TABLE_NAME,value,KEY + " = ?", new String[]{ String.valueOf(e.getId())}); } public ArrayList selectionnerParNom(String nom) { ArrayList alExercices = new ArrayList<>(); Cursor c = db.rawQuery("select * from " + TABLE_NAME + " where "+NAME + " = ? ",new String[]{ nom }); while(c.moveToNext()){ Exercice exo = new Exercice(c.getInt(0), c.getString(1), c.getInt(2), c.getInt(3)); alExercices.add(exo); } c.close(); return alExercices; } public ArrayList selectionnerTout() { ArrayList alExercices = new ArrayList<>(); Cursor c = db.rawQuery("select * from " + TABLE_NAME,null ); while(c.moveToNext()){ Exercice exo = new Exercice(c.getInt(0), c.getString(1), c.getInt(2), c.getInt(3)); alExercices.add(exo); } c.close(); return alExercices; } }