public class PromocaoDAO {
private static final String DATABASE_NAME = "app_promocoes.db";
private static final int VERSAO_BANCO = 2;
private static final String TABLE_PROMOCOES = "fs_promocoes";
public static final String PROMO_ID = "id";
public static final String PROMO_DESCRICAO = "descricao";
public static final String PROMO_STATUS = "status";
public static final String PROMO_INICIO = "inicio";
public static final String PROMO_TERMINO = "termino";
private DatabaseHelper dbHelper;
private SQLiteDatabase db;
private final Context mCtx;
private static final String TABELA_PROMO =
"CREATE TABLE if not exists " + TABLE_PROMOCOES + " (" +
PROMO_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
PROMO_DESCRICAO + "," +
PROMO_INICIO + "," +
PROMO_TERMINO + "," +
PROMO_STATUS + "," +
" UNIQUE (" + PROMO_ID +"));";
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, VERSAO_BANCO);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABELA_PROMO);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROMOCOES);
onCreate(db);
}
}
public PromocaoDAO(Context ctx){
this.mCtx = ctx;
}
public PromocaoDAO open() throws SQLException{
dbHelper = new DatabaseHelper(mCtx);
db = dbHelper.getWritableDatabase();
return this;
}
public void close(){
if(dbHelper != null){
dbHelper.close();
}
}
public long inserirPromocao(String descricapo, String inicio, String termino){
ContentValues values = new ContentValues();
values.put(PROMO_DESCRICAO, descricapo);
values.put(PROMO_INICIO, inicio);
values.put(PROMO_TERMINO, termino);
values.put(PROMO_STATUS, 1);
return db.insert(TABLE_PROMOCOES, null, values);
}
public Cursor buscarTodasPromocoes(){
Cursor mCursor;
mCursor = db.query(TABLE_PROMOCOES, new String[] {PROMO_DESCRICAO, PROMO_INICIO, PROMO_TERMINO},null,null,null,null,null);
if (mCursor != null){
mCursor.moveToFirst();
}
return mCursor;
}
public void testeInsertPromocao(){
inserirPromocao("PROMOCAO DE NATAL", "10/11/2016", "31/12/2016");
inserirPromocao("PROMOCAO DE PESCOA", "10/11/2016", "31/12/2016");
}