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
| package com.hascoet.ubo_blocnote.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import com.hascoet.ubo_blocnote.Note;
import java.util.ArrayList;
import java.util.List;
public class NotesDB {
// Database fields
private SQLiteDatabase database;
private DatabaseHandler dbHandler;
private String[] allColumns = { DatabaseHandler.NOTE_KEY,DatabaseHandler.NOTE_TITRE,
DatabaseHandler.NOTE_DESCRIPTION,DatabaseHandler.NOTE_IMPORTANCE,DatabaseHandler.NOTE_CREATION,DatabaseHandler.NOTE_DATE};
// Champs SQL
public static final String KEY = "_id";
public static final String TITRE = "titre";
public static final String DESCRIPTION = "description";
public static final String IMPORTANCE = "importance";
public static final String DATE = "datealarme";
public static final String CREATION = "datecreation";
public static final String TABLE_NAME = "Notes";
public NotesDB(Context context)
{
dbHandler = new DatabaseHandler(context);
}
public void open() throws SQLException
{
database = dbHandler.getWritableDatabase();
}
public void close()
{
dbHandler.close();
}
/**
* Ajoute une note dans la base de données
* @param n la note à ajouter à la base
*/
public void ajouter(Note n) {
ContentValues value = new ContentValues();
value.put(NotesDB.TITRE, n.getTitre().trim());
value.put(NotesDB.DESCRIPTION, n.getDescription().trim());
value.put(NotesDB.IMPORTANCE,n.getImportance());
value.put(NotesDB.CREATION,n.getDateCreationString());
value.put(NotesDB.DATE,n.getDateString());
database.insert(NotesDB.TABLE_NAME, null, value);
}
/**
* supprimer une note avec son ID
* @param id l'identifiant de la note à supprimer
*/
public int supprimer(int id) {
int retour = database.delete(TABLE_NAME, KEY + " = ?",new String[]{String.valueOf(id)});
return retour;
}
/**
* supprimer une note avec son titre
* @param note a supprimer
*/
public int supprimer(Note note)
{
int retour = database.delete(TABLE_NAME, TITRE + " = ?",new String[]{note.getTitre().trim()});
return retour;
}
/**
* modifier une note
* @param n la note à modifier
*/
public void modifier(Note n) {
ContentValues value = new ContentValues();
value.put(NotesDB.TITRE, n.getTitre().trim());
value.put(NotesDB.DESCRIPTION, n.getDescription().trim());
value.put(NotesDB.IMPORTANCE,n.getImportance());
value.put(NotesDB.DATE,n.getDateString());
database.update(NotesDB.TABLE_NAME,value,"_id"+"="+n.getId(),null);
}
/**
* Selectionne une note avec son ID
* @param id l'identifiant de la note à récupérer
*/
public Note selectionner(int id) {
Cursor c = database.rawQuery("SELECT * from " + TABLE_NAME +
" where " + KEY + " = ? ;" ,new String[]{String.valueOf(id)});
c.moveToFirst();
int id_note = c.getInt(0);
String titre = c.getString(1);
String descrition = c.getString(2);
int importance = c.getInt(3);
String dateCreation = c.getString(4);
String dateAlarme = c.getString(5);
Note n = new Note(id_note,titre,descrition,importance,dateCreation,dateAlarme);
return n;
}
/**
* renvoie un list de toute les notes de la base de donnée
*/
public List<Note> getAll()
{
List<Note> notes = new ArrayList<Note>();
Cursor cursor = database.query(DatabaseHandler.NOTE_TABLE_NAME,
allColumns,null,null,null,null,null);
while(cursor.moveToNext())
{
Note note = new Note(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getInt(3),cursor.getString(4),cursor.getString(5));
notes.add(note);
}
// fermer le cursor
cursor.close();
return notes;
}
/**
* remettre la table à 0
*/
public void reset()
{
database.execSQL(dbHandler.NOTE_TABLE_DROP);
dbHandler.onCreate(database);
}
} |
Partager