StringSQLite.java 3.14 KB
package com.example.martin.projetv5;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by martin on 21/02/2017.
 */

public class StringSQLite extends SQLiteOpenHelper {
    private static final int VERSION_BDD = 1;
    private static final String NOM_BDD = "evenements.db";
    private static StringSQLite sInstance;

    private static final String TABLE_EVENTS = "table_events";
    private static final String TABLE_PRISES = "table_prises";
    private static final String TABLE_MEDOCS = "table_medocs";

    private static final String COL_ID_EV = "ID_ev";
    private static final String COL_DATE = "Date";
    private static final String COL_HEURE_EV = "Heure_ev";
    private static final String COL_EVENT = "Event";

    private static final String COL_ID_P = "ID_prise";
    private static final String COL_MEDOC = "Medoc";
    private static final String COL_HEURE = "Heure";
    private static final String COL_BOOL = "Bool";

    private static final String COL_ID_M = "ID_medoc";
    private static final String COL_NOM1 = "Nom1";
    private static final String COL_NOM2 = "Nom2";
    private static final String COL_POSO = "Posologies";
    private static final String COL_INFOS = "Indications";
    private static final String COL_LOGO = "Logo";

    private static final String CREATE_BDD = "CREATE TABLE " + TABLE_EVENTS + " ("
            + COL_ID_EV + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_DATE + " TEXT NOT NULL, "
            + COL_HEURE_EV + " TEXT NOT NULL, " + COL_EVENT + " TEXT NOT NULL);";

    private static final String CREATE_BDD_PRISES = "CREATE TABLE " + TABLE_PRISES + " ("
            + COL_ID_P + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_MEDOC + " TEXT NOT NULL, "
            + COL_HEURE + " TEXT NOT NULL, " + COL_BOOL + " TEXT NOT NULL);";

    private static final String CREATE_BDD_MEDOCS = "CREATE TABLE " + TABLE_MEDOCS + " ("
            + COL_ID_M + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_NOM1 + " TEXT NOT NULL, "
            + COL_NOM2 + " TEXT NOT NULL, " + COL_POSO + " TEXT NOT NULL, " + COL_INFOS + " TEXT NOT NULL, "
            + COL_LOGO + " TEXT NOT NULL);";

    public static synchronized StringSQLite getInstance(Context context){
        if(sInstance == null){sInstance = new StringSQLite(context);}
        return sInstance;
    }

    public StringSQLite(Context context){
        super(context, NOM_BDD, null, VERSION_BDD);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //on crée la table à partir de la requête écrite dans la variable CREATE_BDD
        db.execSQL(CREATE_BDD);
        db.execSQL(CREATE_BDD_PRISES);
        db.execSQL(CREATE_BDD_MEDOCS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //On supprime la table puis la recrée afin de faire repartir les id à 0
        db.execSQL("DROP TABLE " + TABLE_EVENTS + ";");
        db.execSQL("DROP TABLE " + TABLE_PRISES + ";");
        db.execSQL("DROP TABLE " + TABLE_MEDOCS + ";");
        onCreate(db);
    }
}