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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
| package com.example.medichome;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
public class DBAdapterAliment
{
private static final int NUM_KEY = 0;
private static final int NUM_TITRE = 1;
private static final int NUM_IMAGE = 2;
private static final int NUM_DANSLIST = 3;
private static final int NUM_TYPE = 4;
public static final String KEY_ROWID = "_id";
public static final String KEY_TITRE = "titre";
public static final String KEY_IMAGE = "image";
public static final String KEY_DANSLIST = "danslist";
public static final String KEY_TYPE = "type";
private static final String TAG = "DBAdapterAliment";
private static Context context;
private static final String DATABASE_NAME = "aliment_database.db";
private static final String DATABASE_TABLE = "aliments";
private static int DATABASE_VERSION = 1;
public static final String titre1 = "\"Pommes\"";
public static final String titre2 = "\"Oranges\"";
public static final String titre3 = "\"Bananes\"";
public static final String titre4 = "\"Fraises\"";
public static final String titre5 = "\"Kiwis\"";
public static final String titre6 = "\"Tomates\"";
public static final String titre7 = "\"Haricots\"";
public static final String titre8 = "\"Petit pois\"";
public static final String titre9 = "\"Salade\"";
public static final String titre10 = "\"Endives\"";
public static final String titre11 = "\"Oeufs\"";
public static final String titre12 = "\"Jambon\"";
public static final String titre13 = "\"Poulet\"";
public static final String titre14 = "\"Poisson\"";
public static final String titre15 = "\"Boeuf\"";
public static final String titre16 = "\"Eau\"";
public static final String titre17 = "\"Sel\"";
public static final String titre18 = "\"Poivre\"";
public static final String titre19 = "\"Beurre\"";
public static final String titre20 = "\"Lait\"";
public static final Bitmap image1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.pomme);
public static final Bitmap image2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.orange);
public static final Bitmap image3 = BitmapFactory.decodeResource(context.getResources(), R.drawable.banane);
public static final Bitmap image4 = BitmapFactory.decodeResource(context.getResources(), R.drawable.fraise);
public static final Bitmap image5 = BitmapFactory.decodeResource(context.getResources(), R.drawable.kiwi);
public static final Bitmap image6 = BitmapFactory.decodeResource(context.getResources(), R.drawable.tomate);
public static final Bitmap image7 = BitmapFactory.decodeResource(context.getResources(), R.drawable.haricot);
public static final Bitmap image8 = BitmapFactory.decodeResource(context.getResources(), R.drawable.petitp);
public static final Bitmap image9 = BitmapFactory.decodeResource(context.getResources(), R.drawable.salade);
public static final Bitmap image10 = BitmapFactory.decodeResource(context.getResources(), R.drawable.endive);
public static final Bitmap image11 = BitmapFactory.decodeResource(context.getResources(), R.drawable.oeufs);
public static final Bitmap image12 = BitmapFactory.decodeResource(context.getResources(), R.drawable.jambon);
public static final Bitmap image13 = BitmapFactory.decodeResource(context.getResources(), R.drawable.poulet);
public static final Bitmap image14 = BitmapFactory.decodeResource(context.getResources(), R.drawable.poisson);
public static final Bitmap image15 = BitmapFactory.decodeResource(context.getResources(), R.drawable.boeuf);
public static final Bitmap image16 = BitmapFactory.decodeResource(context.getResources(), R.drawable.eau);
public static final Bitmap image17 = BitmapFactory.decodeResource(context.getResources(), R.drawable.sel);
public static final Bitmap image18 = BitmapFactory.decodeResource(context.getResources(), R.drawable.poivre);
public static final Bitmap image19 = BitmapFactory.decodeResource(context.getResources(), R.drawable.beurre);
public static final Bitmap image20 = BitmapFactory.decodeResource(context.getResources(), R.drawable.lait);
public static final int type0 = 0;
public static final int type1 = 1;
public static final int type2 = 2;
public static final int type3 = 3;
public static final String INSERT1 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre1 + "," + image1 + "1," + type0 + ");";
public static final String INSERT2 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre2 + "," + image2 + "1," + type0 + ");";
public static final String INSERT3 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre3 + "," + image3 + "1," + type0 + ");";
public static final String INSERT4 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre4 + "," + image4 + "1," + type0 + ");";
public static final String INSERT5 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre5 + "," + image5 + "1," + type0 + ");";
public static final String INSERT6 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre6 + "," + image6 + "1," + type1 + ");";
public static final String INSERT7 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre7 + "," + image7 + "1," + type1 + ");";
public static final String INSERT8 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre8 + "," + image8 + "1," + type1 + ");";
public static final String INSERT9 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre9 + "," + image9 + "1," + type1 + ");";
public static final String INSERT10 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre10 + "," + image10 + "1," + type1 + ");";
public static final String INSERT11 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre11 + "," + image11 + "1," + type2 + ");";
public static final String INSERT12 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre12 + "," + image12 + "1," + type2 + ");";
public static final String INSERT13 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre13 + "," + image13 + "1," + type2 + ");";
public static final String INSERT14 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre14 + "," + image14 + "1," + type2 + ");";
public static final String INSERT15 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre15 + "," + image15 + "1," + type2 + ");";
public static final String INSERT16 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre16 + "," + image16 + "1," + type3 + ");";
public static final String INSERT17 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre17 + "," + image17 + "1," + type3 + ");";
public static final String INSERT18 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre18 + "," + image18 + "1," + type3 + ");";
public static final String INSERT19 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre19 + "," + image19 + "1," + type3 + ");";
public static final String INSERT20 = "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre20 + "," + image20 + "1," + type3 + ");";
private static final String DATABASE_CREATE =
"create table aliments (_id integer primary key autoincrement, titre text not null,"
+ "image blob, danslist integer, type interger);";
private DatabaseHelperAliment DBHelper;
private SQLiteDatabase db;
public DBAdapterAliment(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelperAliment(context);
}
private static class DatabaseHelperAliment extends SQLiteOpenHelper
{
DatabaseHelperAliment(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
System.out.println("DatabaseHelperAliment");
}
@Override
public void onCreate(SQLiteDatabase db)
{
System.out.println(" OnCreate ");
db.execSQL(DATABASE_CREATE);
db.execSQL(INSERT1);
db.execSQL(INSERT2);
db.execSQL(INSERT3);
db.execSQL(INSERT4);
db.execSQL(INSERT5);
db.execSQL(INSERT6);
db.execSQL(INSERT7);
db.execSQL(INSERT8);
db.execSQL(INSERT9);
db.execSQL(INSERT10);
db.execSQL(INSERT11);
db.execSQL(INSERT12);
db.execSQL(INSERT13);
db.execSQL(INSERT14);
db.execSQL(INSERT15);
db.execSQL(INSERT16);
db.execSQL(INSERT17);
db.execSQL(INSERT18);
db.execSQL(INSERT19);
db.execSQL(INSERT20);
System.out.println(" 20 INSERT ");
Log.w("TEST : ", "PREMIERE CREATION DE LA BASE ALIMENT !");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
System.out.println(" OnUpgrade ");
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS aliments");
onCreate(db);
}
}
public DBAdapterAliment open() throws SQLException
{
//DATABASE_VERSION++;
db = DBHelper.getWritableDatabase();
System.out.println(".open() OUVERTURE BD");
return this;
}
//---closes the database---
public void close()
{
System.out.println(".close() FERMETURE BD");
DBHelper.close();
}
//---insert a title into the database---
public long insertAliment(String t, byte[] image, int dl, int ty)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITRE, t);
initialValues.put(KEY_IMAGE, image);
initialValues.put(KEY_DANSLIST, dl);
initialValues.put(KEY_TYPE, ty);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular title---
public boolean deleteAliment(long rowId)
{
boolean i = db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
if (i) System.out.println("Aliment supprimé");
return i;
}
public boolean deleteAliment(String t)
{
boolean i = db.delete(DATABASE_TABLE, KEY_TITRE +" LIKE '" + t + "'", null) > 0;
if (i) System.out.println("Aliment supprimé");
return i;
}
//---retrieves all the titles---
public Cursor getAllAliment()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_TITRE,
KEY_IMAGE,
KEY_DANSLIST,
KEY_TYPE},
null,
null,
null,
null,
null);
}
//---retrieves a particular title---
public Cursor getAliment(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_TITRE,
KEY_IMAGE,
KEY_DANSLIST,
KEY_TYPE},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getAllAlimentByDansList(int dl) throws SQLException
{
Cursor mCursor = db.rawQuery("select * from aliments where danslist = " + dl, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
else { System.out.println("Cursor vide");}
return mCursor;
}
//---updates a title---
public boolean updateAliment(long rowId, String t, byte[] image, int dl, int ty)
{
ContentValues args = new ContentValues();
args.put(KEY_TITRE, t);
args.put(KEY_IMAGE, image);
args.put(KEY_DANSLIST, dl);
args.put(KEY_TYPE, ty);
return db.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}
public long insertAliment(UnAliment al)
{
//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(KEY_TITRE, al.getTitre());
values.put(KEY_IMAGE, al.getImage());
values.put(KEY_DANSLIST, al.getDansList());
values.put(KEY_TYPE, al.getType());
System.out.println("Aliment inseré");
//on insère l'objet dans la BDD via le ContentValues
return db.insert(DATABASE_TABLE, null, values);
}
public int updateAliment(long l, UnAliment al)
{
//La mise à jour d'un livre 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(KEY_TITRE, al.getTitre());
values.put(KEY_IMAGE, al.getImage());
values.put(KEY_DANSLIST, al.getDansList());
values.put(KEY_TYPE, al.getType());
return db.update(DATABASE_TABLE, values, KEY_ROWID + " = " +l, null);
}
public int removeAlimentWithID(long l)
{
//Suppression d'un livre de la BDD grâce à l'ID
return db.delete(DATABASE_TABLE, KEY_ROWID + " = " +l, null);
}
public UnAliment getAlimentWithTitre(String titre)
{
//Récupère dans un Cursor les valeur correspondant à un livre contenu dans la BDD (ici on sélectionne le livre grâce à son titre)
Cursor c = db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITRE, KEY_IMAGE, KEY_DANSLIST, KEY_TYPE}, KEY_TITRE + " LIKE \"" + titre +"\"", null, null, null, null);
return cursorToAliment(c);
}
public UnAliment getAlimentWithId(int id)
{
//Récupère dans un Cursor les valeur correspondant à un livre contenu dans la BDD (ici on sélectionne le livre grâce à son titre)
Cursor c = db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITRE, KEY_IMAGE, KEY_DANSLIST, KEY_TYPE}, KEY_ROWID + " LIKE \"" + id +"\"", null, null, null, null);
return cursorToAliment(c);
}
public UnAliment getAlimentWithDansList(int dl)
{
//Récupère dans un Cursor les valeur correspondant à un livre contenu dans la BDD (ici on sélectionne le livre grâce à son titre)
Cursor c = db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITRE, KEY_IMAGE, KEY_DANSLIST, KEY_TYPE}, KEY_DANSLIST + " = \"" + dl +"\"", null, null, null, null);
return cursorToAliment(c);
}
//Cette méthode permet de convertir un cursor en un livre
private UnAliment cursorToAliment(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 livre
UnAliment al = new UnAliment();
//on lui affecte toutes les infos grâce aux infos contenues dans le Cursor
al.setIdAl(c.getInt(NUM_KEY));
al.setTitre(c.getString(NUM_TITRE));
al.setImage(c.getBlob(NUM_IMAGE));
al.setDansList(c.getInt(NUM_DANSLIST));
al.setType(c.getInt(NUM_TYPE));
//On ferme le cursor
c.close();
//On retourne le livre
return al;
}
} |
Partager