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
| package com.example.wamove; /**
* Created by Oier on 10/01/2015.
*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class CommandesBDD {
private static final int VERSION_BDD = 1;
private static final String NOM_BDD = "commandes.db";
private static final String TABLE_COMMANDES = "table_commandes";
private static final String COL_ID = "ID";
private static final int NUM_COL_ID = 0;
private static final String COL_DATE = "Date";
private static final int NUM_COL_DATE = 1;
private static final String COL_HEURE = "Heure";
private static final int NUM_COL_HEURE = 2;
private static final String COL_NBR_POTS = "Nbr_pots";
private static final int NUM_COL_NBR_POTS = 3;
private static final String COL_POURCENTAGE_ROUGE = "Rouge";
private static final int NUM_COL_POURCENTAGE_ROUGE = 4;
private static final String COL_POURCENTAGE_BLEU = "Bleu";
private static final int NUM_COL_POURCENTAGE_BLEU = 5;
private static final String COL_POURCENTAGE_VERT = "Vert";
private static final int NUM_COL_POURCENTAGE_VERT = 6;
private SQLiteDatabase bdd;
private MaBaseSQLite maBaseSQLite;
public CommandesBDD(Context context){
//On créer la BDD et sa table
maBaseSQLite = new MaBaseSQLite(context, NOM_BDD, null, VERSION_BDD);
}
public void open(){
//on ouvre la BDD en écriture
bdd = maBaseSQLite.getWritableDatabase();
}
public void close(){
//on ferme l'accès à la BDD
bdd.close();
}
public SQLiteDatabase getBDD(){
return bdd;
}
public long insertCommande(Commande Commande){
//Création d'un ContentValues (fonctionne comme une HashMap)
ContentValues values = new ContentValues();
//on lui ajoute une valeur associé à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
values.put(COL_DATE, Commande.getDate());
values.put(COL_HEURE, Commande.getHeure());
values.put(COL_NBR_POTS, Commande.getNbr_pots());
values.put(COL_POURCENTAGE_ROUGE, Commande.getPourcentage_rouge());
values.put(COL_POURCENTAGE_BLEU, Commande.getPourcentage_bleu());
values.put(COL_POURCENTAGE_VERT, Commande.getPourcentage_vert());
//on insère l'objet dans la BDD via le ContentValues
return bdd.insert(TABLE_COMMANDES, null, values);
}
public int updateCommande(int id, Commande Commande){
//La mise à jour d'une commande dans la BDD fonctionne plus ou moins comme une insertion
//il faut simple préciser quelle livre on doit mettre à jour grâce à l'ID
ContentValues values = new ContentValues();
values.put(COL_DATE, Commande.getDate());
values.put(COL_HEURE, Commande.getHeure());
values.put(COL_NBR_POTS, Commande.getNbr_pots());
values.put(COL_POURCENTAGE_ROUGE, Commande.getPourcentage_rouge());
values.put(COL_POURCENTAGE_BLEU, Commande.getPourcentage_bleu());
values.put(COL_POURCENTAGE_VERT, Commande.getPourcentage_vert());
return bdd.update(TABLE_COMMANDES, values, COL_ID + " = " +id, null);
}
public int removeCommandeWithID(int id){
//Suppression d'une commande de la BDD grâce à l'ID
return bdd.delete(TABLE_COMMANDES, COL_ID + " = " +id, null);
}
//Cette méthode permet de convertir un cursor en une commande
private Commande cursorToLivre(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éé une commande
Commande commande = new Commande();
//on lui affecte toutes les infos grâce aux infos contenues dans le Cursor
commande.setId(c.getInt(NUM_COL_ID));
commande.setDate(c.getString(NUM_COL_DATE));
commande.setHeure(c.getString(NUM_COL_HEURE));
commande.setNbr_pots(c.getInt(NUM_COL_NBR_POTS));
commande.setPoucentage_rouge(c.getInt(NUM_COL_POURCENTAGE_ROUGE));
commande.setPourcentage_bleu(c.getInt(NUM_COL_POURCENTAGE_BLEU));
commande.setPourcentage_vert(c.getInt(NUM_COL_POURCENTAGE_VERT));
//On ferme le cursor
c.close();
//On retourne la commande
return commande;
}
} |
Partager