1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
package com.example.jassimaanaoui.bddtest;
/**
* Created by jassim.aanaoui on 12/02/2018.
*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class ExerciceBDD {
private static final int VERSION_BDD = 1;
private static final String NOM_BDD = "Sportif.db";
private static final String TABLE_EXERCICE = "table_exercice";
private static final String COL_ID = "ID";
private static final int NUM_COL_ID = 0;
private static final String COL_LOGIN = "Login";
private static final int NUM_COL_LOGIN = 1;
private static final String COL_FORCEAV = "ForceAvant";
private static final int NUM_COL_FORCEAV = 2;
private static final String COL_FORCEAR = "ForceArriere";
private static final int NUM_COL_FORCEAR = 3;
private SQLiteDatabase bdd;
private DataManager dataManager;
public ExerciceBDD(Context context ) {
//On crée la BDD et sa table
dataManager = new DataManager(context, NOM_BDD, null, VERSION_BDD);
}
public void open(){
//on ouvre la BDD en écriture
bdd = dataManager.getWritableDatabase();
}
public void close(){
//on ferme l'accès à la BDD
bdd.close();
}
public SQLiteDatabase getBDD() {
return bdd;
}
public long insertExercice(Exercice exercice){
//Création d'un ContentValues (fonctionne comme une HashMap)
ContentValues values = new ContentValues();
//on lui ajoute une valeur associée à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
values.put(COL_LOGIN, exercice.getLogin());
values.put(COL_FORCEAV, exercice.getForce_avant());
values.put(COL_FORCEAR, exercice.getForce_arriere());
//on insère l'objet dans la BDD via le ContentValues
return bdd.insert(TABLE_EXERCICE, null, values);
}
public int updateExercice(int id, Exercice exercice){
//La mise à jour d'un livre dans la BDD fonctionne plus ou moins comme une insertion
//il faut simplement préciser quel livre on doit mettre à jour grâce à l'ID
ContentValues values = new ContentValues();
values.put(COL_LOGIN, exercice.getLogin());
values.put(COL_FORCEAV, exercice.getForce_avant());
values.put(COL_FORCEAR, exercice.getForce_arriere());
return bdd.update(TABLE_EXERCICE, values, COL_ID + " = " +id, null);
}
public int removeExerciceWithID(int id){
//Suppression d'un livre de la BDD grâce à l'ID
return bdd.delete(TABLE_EXERCICE, COL_ID + " = " +id, null);
}
public Exercice getExerciceWithLogin(String login){
//Récupère dans un Cursor les valeurs correspondant à un exercice contenu dans la BDD (ici on sélectionne le l'exercice grâce au login)
Cursor c = bdd.query(TABLE_EXERCICE, new String[] {COL_ID, COL_LOGIN, COL_FORCEAV, COL_FORCEAR}, COL_LOGIN + " LIKE \"" + login +"\"", null, null, null, null);
return cursorToExercice(c);
}
//Cette méthode permet de convertir un cursor en un exercice
private Exercice cursorToExercice(Cursor c) {
//si aucun élément n'a été retourné dans la requête, on renvoie null
if (c.getCount() == 0)
return null;
//Sinon on se place sur le premier élément
c.moveToFirst();
//On créé un exercice
Exercice exercice = new Exercice();
//on lui affecte toutes les infos grâce aux infos contenues dans le Cursor
exercice.setId(c.getInt(NUM_COL_ID));
exercice.setLogin(c.getString(NUM_COL_LOGIN));
exercice.setForce_avant(c.getInt(NUM_COL_FORCEAV));
exercice.setForce_arriere(c.getInt(NUM_COL_FORCEAR));
//On ferme le cursor
c.close();
//On retourne l'exercice
return exercice;
}
} |
Partager