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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
public class SQLiteAdapter {
public static final String MYDATABASE_NAME = "DettesManager";
public static final int MYDATABASE_VERSION = 7;
/*
* 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_BUDGETALLOUE = "categorieBudgetAlloue";
public static final String CATEGORIE_DATEPREVBUDGET = "categorieDatePrevBudget";
public static final String CATEGORIE_DATEMODIF = "categorieDateModif";
/* Table Solde << Disponible à partir de la db_v7 >> */
public static final String TABLE_SOLDE = "SoldeCompte";
public static final String SOLDE_ID = "_id";
public static final String SOLDE_DATE = "soldeDate";
public static final String SOLDE_SOLDE = "soldeSolde";
public static final String SOLDE_MOIS = "soldeMois";
public static final String SOLDE_ANNEE = "soldeAnnee"; //Ajouté dans la base v7
/* Table Budget */
/* Section Gestion Budgétaire -> A partir de la base v7 */
public static final String TABLE_BUDGET = "Budget";
public static final String BUDGET_ID = "_id";
public static final String BUDGET_NOM = "budgetNom";
public static final String BUDGET_COMMENTAIRE = "budgetCommentaire";
public static final String BUDGET_MOIS = "budgetMois";
public static final String BUDGET_ANNEE = "budgetAnnee";
public static final String BUDGET_BUDGETALLOUE = "budgetAlloue";
public static final String BUDGET_DATEPREV = "budgetDatePrev";
public static final String BUDGET_DATEMODIF = "budgetDateModif";
/*
* SCRIPTS DE CREATION DES TABLES
*/
/* Script de création de la table Depense */
private static final String SCRIPT_CREATE_TABLE_DEPENSE =
"create table "
+ TABLE_DEPENSE
+ " ("
+ DEPENSE_ID + " integer primary key autoincrement, "
+ DEPENSE_DATEDEPENSE + " text not null, "
+ DEPENSE_DATESOLDE + " text not null, "
+ DEPENSE_OPERATION + " text not null, "
+ DEPENSE_CATEGORIE + " text not null, "
+ DEPENSE_MONTANT + " text not null, "
+ DEPENSE_MOYEN + " text not null, "
+ DEPENSE_COMMENTAIRE + " text not null, "
+ DEPENSE_ARCHIVE + " text not null, "
+ DEPENSE_ETAT + " text not null, "
+ DEPENSE_MOIS + " text not null, "
+ DEPENSE_ANNEE + " text not null);";
/* 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);" ;
/* Script de création de la table Budget */
private static final String SCRIPT_CREATE_TABLE_BUDGET =
"create table "
+ TABLE_BUDGET
+ " ("
+ BUDGET_ID + " integer primary key autoincrement, "
+ BUDGET_NOM + " text not null, "
+ BUDGET_COMMENTAIRE + " text not null, "
+ BUDGET_MOIS + " text not null, "
+ BUDGET_ANNEE + " text not null, "
+ BUDGET_BUDGETALLOUE + " text not null, "
+ BUDGET_DATEPREV + " text not null, "
+ BUDGET_DATEMODIF + " text not null);";
/* Script de création de la table Solde */
private static final String SCRIPT_CREATE_TABLE_SOLDE =
"create table "
+ TABLE_SOLDE
+ " ("
+ SOLDE_ID + " integer primary key autoincrement, "
+ SOLDE_DATE + " text not null, "
+ SOLDE_SOLDE + " text not null, "
+ SOLDE_MOIS + " text not null, "
+ SOLDE_ANNEE + " 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();
}
public void initDbVers7(){
// Initialiser la BDD en version 7 : Récupérer les noms de categories et inserer dans la table budget en allant de janvier 2014 à décembre 2022
Log.d("Init...","Init");
String Mois = "";
int Annee = 2014;
int iMois = 0;
String auj = DateCourante();
for (Annee = 2014;Annee<2016;Annee++){
for(iMois=1;iMois<=12;iMois++){
switch(iMois){
case 1:
Mois = "Janvier";
break;
case 2:
Mois = "Fevrier";
break;
case 3:
Mois = "Mars";
break;
case 4:
Mois = "Avril";
break;
case 5:
Mois = "Mai";
break;
case 6:
Mois = "Juin";
break;
case 7:
Mois = "Juillet";
break;
case 8:
Mois = "Aout";
break;
case 9:
Mois = "Septembre";
break;
case 10:
Mois = "Octobre";
break;
case 11:
Mois = "Novembre";
break;
case 12:
Mois = "Decembre";
break;
}
String ReqSQLINIT = "INSERT INTO Budget (budgetNom,budgetCommentaire,budgetMois,budgetAnnee,budgetAlloue,budgetDatePrev,budgetDateModif) SELECT categorieNom, 'Insertion Auto',Mois,Annee,'','',auj FROM Categorie WHERE 1";
Log.d("ReqSQLINIT",ReqSQLINIT);
db.execSQL(ReqSQLINIT);
}
}
}
@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 < 6) {
//Ajout de la table mail
Toast.makeText(context, "Passage de la base en version v6...", 5);
db.execSQL("DROP TABLE IF EXISTS "+MYDATABASE_NAME);
final String ALTER_TABLE_CATEGORIE2 = "ALTER TABLE " +TABLE_CATEGORIE+ " ADD COLUMN categorieMois char(99);";
final String ALTER_TABLE_CATEGORIE3 = "ALTER TABLE " +TABLE_CATEGORIE+ " ADD COLUMN categorieBudgetAlloue char(99);";
final String ALTER_TABLE_CATEGORIE4 = "ALTER TABLE " +TABLE_CATEGORIE+ " ADD COLUMN categorieDatePrevBudget char(99);";
final String ALTER_TABLE_CATEGORIE5 = "ALTER TABLE " +TABLE_CATEGORIE+ " ADD COLUMN categorieDateModif char(99);";
db.execSQL(ALTER_TABLE_CATEGORIE2);
db.execSQL(ALTER_TABLE_CATEGORIE3);
db.execSQL(ALTER_TABLE_CATEGORIE4);
db.execSQL(ALTER_TABLE_CATEGORIE5);
db.execSQL(SCRIPT_CREATE_TABLE_BUDGET);
db.execSQL(SCRIPT_CREATE_TABLE_SOLDE);
}
if (oldVersion < 7) {
Toast.makeText(context, "Passage de la base en version v6...", 5);
db.execSQL("ALTER TABLE " +TABLE_DEPENSE+ " ADD COLUMN depenseMois char(99);");
db.execSQL("ALTER TABLE " +TABLE_DEPENSE+ " ADD COLUMN depenseAnnee char(99);");
db.execSQL(SCRIPT_CREATE_TABLE_BUDGET);
db.execSQL(SCRIPT_CREATE_TABLE_SOLDE);
/*Log.d("ALTER_TABLE_DEPENSE1",ALTER_TABLE_DEPENSE1);
Log.d("ALTER_TABLE_DEPENSE2",ALTER_TABLE_DEPENSE2);*/
Log.d("SCRIPT_CREATE_TABLE_BUDGET",SCRIPT_CREATE_TABLE_BUDGET);
Log.d("SCRIPT_CREATE_TABLE_SOLDE",SCRIPT_CREATE_TABLE_SOLDE);
initDbVers7();
}
} |
Partager