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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
public class SQLiteAdapter {
public static final String MYDATABASE_NAME = "DettesManager";
public static final int MYDATABASE_VERSION = 7; //New Table Depense
/********************************************************************
* DEFINITION DES TABLES
********************************************************************/
/* Table Categorie << Disponible à partir de la db_v5 >> */
public static final String TABLE_CATEGORIE = "Categorie";
public static final String CATEGORIE_ID = "_id";
public static final String CATEGORIE_NOM = "categorieNom";
public static final String CATEGORIE_COMMENTAIRE = "categorieCommentaire";
/* Section Gestion Budgétaire -> A partir de la v1.1 */
public static final String CATEGORIE_MOIS = "categorieMois";
public static final String CATEGORIE_ANNEE = "categorieAnnee";
public static final String CATEGORIE_BUDGETALLOUE = "categorieBudgetAlloue";
public static final String CATEGORIE_DATEPREVBUDGET = "categorieDatePrevBudget";
public static final String CATEGORIE_DATEMODIF = "categorieDateModif";
/********************************************************************
* SCRIPTS DE CREATION DES TABLES
********************************************************************/
/* Script de création de la table Categorie */
private static final String SCRIPT_CREATE_TABLE_CATEGORIE =
"create table "
+ TABLE_CATEGORIE
+ " ("
+ CATEGORIE_ID + " integer primary key autoincrement, "
+ CATEGORIE_NOM + " text not null, "
+ CATEGORIE_COMMENTAIRE + " text not null,"
+ CATEGORIE_MOIS + " text not null,"
+ CATEGORIE_BUDGETALLOUE + " text not null,"
+ CATEGORIE_DATEPREVBUDGET + " text not null,"
+ CATEGORIE_DATEMODIF + " text not null);" ;
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase db;
private Context context;
public SQLiteAdapter(Context c){
context = c;
}
/********************************************************************
* GESTION DE LA DB
********************************************************************/
//Ouverture pour lecture de la bdd
public SQLiteAdapter openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
db = sqLiteHelper.getReadableDatabase();
return this;
}
//Ouverture pour ecriture dans la bdd
public SQLiteAdapter openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
db = sqLiteHelper.getWritableDatabase();
return this;
}
public void close(){
sqLiteHelper.close();
}
/* Methode qui liste les categories */
public Cursor ListerCategories(){
String[] columns = new String[]{CATEGORIE_ID, CATEGORIE_NOM, CATEGORIE_COMMENTAIRE, CATEGORIE_MOIS, CATEGORIE_ANNEE, CATEGORIE_BUDGETALLOUE};
Cursor cursorCategories = db.rawQuery("SELECT * FROM "+TABLE_CATEGORIE+" WHERE categorieMois LIKE ''", null);
return cursorCategories;
}
/* Methode qui ajoute des categories */
public long insertCategorie(String Nom, String Commentaire, String Mois, String Annee, String budgetAlloue, String datePrevBudget, String dateModif){
ContentValues contentValues = new ContentValues();
String auj = DateCourante();
contentValues.put(CATEGORIE_NOM, Nom);
contentValues.put(CATEGORIE_COMMENTAIRE, Commentaire);
contentValues.put(CATEGORIE_MOIS, Mois);
contentValues.put(CATEGORIE_ANNEE, Annee);
contentValues.put(CATEGORIE_BUDGETALLOUE, "");
contentValues.put(CATEGORIE_DATEPREVBUDGET, "");
contentValues.put(CATEGORIE_DATEMODIF, auj);
for(long lAnnee=2013;lAnnee<2016;lAnnee++){
for(int iMois=1;iMois<13;iMois++){
String sMois = "";
switch(iMois)
{
case 1: sMois = "Janvier";
break;
case 2: sMois = "Fevrier";
break;
case 3: sMois = "Mars";
break;
case 4: sMois = "Avril";
break;
case 5: sMois = "Mai";
break;
case 6: sMois = "Juin";
break;
case 7: sMois = "Juillet";
break;
case 8: sMois = "Aout";
break;
case 9: sMois = "Septembre";
break;
case 10:sMois = "Octobre";
break;
case 11:sMois = "Novembre";
break;
case 12:sMois = "Decembre";
break;
}
contentValues.put(CATEGORIE_NOM, Nom);
contentValues.put(CATEGORIE_COMMENTAIRE, Commentaire);
contentValues.put(CATEGORIE_MOIS, sMois);
contentValues.put(CATEGORIE_ANNEE, lAnnee);
contentValues.put(CATEGORIE_BUDGETALLOUE, "");
contentValues.put(CATEGORIE_DATEPREVBUDGET, "");
contentValues.put(CATEGORIE_DATEMODIF, auj);
Log.d("InsertCategorie","Categorie : "+Nom+" ajoutée pour "+sMois+" "+lAnnee+".");
}
}
Log.d("Categorie","Categorie ajoutée.");
Toast.makeText(context, "Categorie ajoutée.", Toast.LENGTH_SHORT).show();
return db.insert(TABLE_CATEGORIE, null, contentValues);
}
public void initBudget(){
Cursor c = ListerCategories();
while(c.moveToNext()){
String sCategorie = c.getString(1);
String sMois = c.getString(3);
String sAnnee = c.getString(4);
String EtatBudget = c.getString(5);
Log.d("Categorie",sCategorie);
Log.d("EtatBudget",EtatBudget);
for(long lAnnee = 2013;lAnnee<2016;lAnnee++){
sAnnee = String.valueOf(lAnnee);
this.insertBudget("Janvier", sAnnee, sCategorie, "", "", "");
this.insertBudget("Fevrier", sAnnee, sCategorie, "", "", "");
this.insertBudget("Mars", sAnnee, sCategorie, "", "", "");
this.insertBudget("Avril", sAnnee, sCategorie, "", "", "");
this.insertBudget("Mai", sAnnee, sCategorie, "", "", "");
this.insertBudget("Juin", sAnnee, sCategorie, "", "", "");
this.insertBudget("Juillet", sAnnee, sCategorie, "", "", "");
this.insertBudget("Aout", sAnnee, sCategorie, "", "", "");
this.insertBudget("Septembre", sAnnee, sCategorie, "", "", "");
this.insertBudget("Octobre", sAnnee, sCategorie, "", "", "");
this.insertBudget("Novembre", sAnnee, sCategorie, "", "", "");
this.insertBudget("Decembre", sAnnee, sCategorie, "", "", "");
Log.d("Budget inséré : ","Année : "+sAnnee);
}
}
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Toast.makeText(context, "Création de la base...", Toast.LENGTH_LONG).show();
db.execSQL(SCRIPT_CREATE_TABLE_CATEGORIE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.d("oldVersion",String.valueOf(oldVersion));
Log.d("newVersion",String.valueOf(newVersion));
Toast.makeText(context, "Mise à jour de la base..."+oldVersion+"->"+newVersion+"...Veuillez patienter.", Toast.LENGTH_LONG).show();
if(oldVersion < 7){
//Toast.makeText(context, "Passage de la base en version v7...", 5);
db.execSQL("ALTER TABLE " +TABLE_CATEGORIE+ " ADD COLUMN categorieAnnee char(99);");
initBudget();
}
}
}
} |
Partager