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 124
| 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;
import java.util.*;
public class LecturesBDD {
private static final int VERSION_BDD = 1;
private static final String NOM_BDD = "lecture.db";
public static final int NUM_COL_ID = 0;
private static final int NUM_COL_TYPE = 1;
private static final int NUM_COL_ADRESSE = 2;
private static final int NUM_COL_ETAT = 3;
private static final int NUM_COL_DATE = 4;
private static final int NUM_COL_HEURE = 5;
private SQLiteDatabase bdd;
private MaBaseSQLite maBaseSQLite;
public LecturesBDD(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 insertLecture(Lecture Lecture){
//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(MaBaseSQLite.COL_TYPE, Lecture.getType());
values.put(MaBaseSQLite.COL_ADRESSE, Lecture.getAdresse());
values.put(MaBaseSQLite.COL_ETAT, Lecture.getEtat());
values.put(MaBaseSQLite.COL_DATE, Lecture.getEtat());
values.put(MaBaseSQLite.COL_HEURE, Lecture.getEtat());
//on insère l'objet dans la BDD via le ContentValues
return bdd.insert(MaBaseSQLite.TABLE_LECTURES, null, values);
}
public int updateCommande(int id, Lecture Lecture){
//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(MaBaseSQLite.COL_TYPE, Lecture.getType());
values.put(MaBaseSQLite.COL_ADRESSE, Lecture.getAdresse());
values.put(MaBaseSQLite.COL_ETAT, Lecture.getEtat());
values.put(MaBaseSQLite.COL_DATE, Lecture.getEtat());
values.put(MaBaseSQLite.COL_HEURE, Lecture.getEtat());
return bdd.update(MaBaseSQLite.TABLE_LECTURES, values, MaBaseSQLite.COL_ID + " = " +id, null);
}
public int removeLectureeWithID(int id){
//Suppression d'une commande de la BDD grâce à l'ID
return bdd.delete(MaBaseSQLite.TABLE_LECTURES, MaBaseSQLite.COL_ID + " = " +id, null);
}
public int resetBDD(){
return bdd.delete(MaBaseSQLite.TABLE_LECTURES, null, null);
}
public int nombreDeLigne(){
//sélectionner toutes les lignes de la table lectures
Cursor curseur = bdd.query(MaBaseSQLite.TABLE_LECTURES, null, null, null, null, null, null);
//Renvoyer le nombre de lignes
return curseur.getCount();
}
public ArrayList<Lecture> listeDesLectures(){
ArrayList<Lecture> list_rapport = new ArrayList<Lecture>();
//Déclarer un objet Lecture qui servira de tampon
Lecture c;
//Exécution de la requête de sélection de toutes les lignes contenues dans la table
Cursor curseur = bdd.query(MaBaseSQLite.TABLE_LECTURES, null, null, null, null, null, null);
//Vérifier si le curseur contient au moins une ligne
if (curseur != null) {
//Se placer au premier enregistrement
curseur.moveToFirst();
//Parcourir les enregistrement tant que le dernier n'est pas encore lu (En quelque sorte)...
while (!curseur.isAfterLast()) {
//Créer une commande
c = new Lecture();
//Affecter chaque valeur provenant d'un champ de la base de données à l'objet c
c.setId(curseur.getInt(curseur.getColumnIndex(MaBaseSQLite.COL_ID)));
c.setType(curseur.getString(curseur.getColumnIndex(MaBaseSQLite.COL_TYPE)));
c.setAdresse(curseur.getString(curseur.getColumnIndex(MaBaseSQLite.COL_ADRESSE)));
c.setEtat(curseur.getString(curseur.getColumnIndex(MaBaseSQLite.COL_ETAT)));
c.setDate(curseur.getString(curseur.getColumnIndex(MaBaseSQLite.COL_DATE)));
c.setHeure(curseur.getString(curseur.getColumnIndex(MaBaseSQLite.COL_HEURE)));
//Ajouter la lecture dans la liste
list_rapport.add(c);
//Passer à l'enregistrement suivant
curseur.moveToNext();
}
//Fermer le curseur
curseur.close();
}
//Retourner la liste
return list_rapport;
}
} |
Partager