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
|
package pck.bcdpush;
import java.util.LinkedList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class BaseDeNotif extends SQLiteOpenHelper {
private static final String TABLE_NOTIF = "table_notifications";
private static final String COL_ID = "ID";
private static final String COL_DATE= "Date";
private static final String COL_HEURE = "HEURE";
private static final String COL_LIBELLE = "LIBELLE";
private static final String CREATE_BDD = "CREATE TABLE " + TABLE_NOTIF + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_DATE + " TEXT NOT NULL, "
+ COL_HEURE + " TEXT NOT NULL, " + COL_LIBELLE + "TEXT NOT NULL) ;";
public BaseDeNotif(Context context) {
super(context, TABLE_NOTIF,null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//on créé la table à partir de la requête écrite dans la variable CREATE_BDD
db.execSQL(CREATE_BDD);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE " + TABLE_NOTIF + ";");
onCreate(db);
}
public void addNotif(Notif notif){
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put(COL_DATE, notif.getDate().toString()); // get title
values.put(COL_HEURE, notif.getHeure().toString()); // get author
values.put(COL_LIBELLE, notif.getLibelle());
// 3. insert
db.insert(TABLE_NOTIF,null,values);
// 4. close
db.close();
}
public List<Notif> getAllNotif() {
List<Notif> notifs = new LinkedList<Notif>();
// 1. build the query
String query = "SELECT * FROM " + TABLE_NOTIF ;
// 2. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
// 3. go over each row, build notif and add it to list
Notif notif = null;
if (cursor.moveToFirst()) {
do {
notif = new Notif();
notif.setId(Integer.parseInt(cursor.getString(0)));
notif.setDate(cursor.getString(1));
notif.setHeure(cursor.getString(2));
notif.setHeure(cursor.getString(3));
// Add notif to notifs
notifs.add(notif);
} while (cursor.moveToNext());
}
// return notifs
return notifs;
}
} |
Partager