Skip to content

Instantly share code, notes, and snippets.

@Vitorspk
Created June 18, 2015 02:17
Show Gist options
  • Save Vitorspk/a3406e96ee0806687b92 to your computer and use it in GitHub Desktop.
Save Vitorspk/a3406e96ee0806687b92 to your computer and use it in GitHub Desktop.
Android
package amigosecreto.hp.example.com.db;
/**
* Created by HP on 17/06/2015.
*/
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class DesejoDAO {
private MySQLiteOpenHelper helper;
private SQLiteDatabase database;
public DesejoDAO(Context ctx) {
helper = new MySQLiteOpenHelper(ctx);
}
public void open() throws SQLException{
database = helper.getWritableDatabase();
}
public void close(){
helper.close();
}
public final void alterar(Desejo old_desejo, Desejo new_desejo){
ContentValues values = new ContentValues();
values.put(helper.COLUMN_ID, old_desejo.getId());
values.put(helper.COLUMN_PRODUTO, new_desejo.getProduto());
values.put(helper.COLUMN_CATEGORIA, new_desejo.getCategoria());
values.put(helper.COLUMN_PRECO_MINIMO, new_desejo.getPrecoMinimo());
values.put(helper.COLUMN_PRECO_MAXIMO, new_desejo.getPrecoMaximo());
values.put(helper.COLUMN_LOJAS, new_desejo.getLojas());
database.update(helper.TABLE_DESEJO, values, helper.COLUMN_ID+" = "+old_desejo.getId(), null);
}
public final void inserir(Desejo desejo){
ContentValues values = new ContentValues();
values.put(helper.COLUMN_ID, desejo.getId());
values.put(helper.COLUMN_PRODUTO, desejo.getProduto());
values.put(helper.COLUMN_CATEGORIA, desejo.getCategoria());
values.put(helper.COLUMN_PRECO_MINIMO, desejo.getPrecoMinimo());
values.put(helper.COLUMN_PRECO_MAXIMO, desejo.getPrecoMaximo());
values.put(helper.COLUMN_LOJAS, desejo.getLojas());
database.insert(helper.TABLE_DESEJO, null, values);
}
public final void remover(Desejo desejo){
database.delete(helper.TABLE_DESEJO, helper.COLUMN_ID+" = "+desejo.getId(), null);
}
public final List<Desejo> listar(){
ArrayList<Desejo> lista = new ArrayList<Desejo>();
Cursor cursor = database.rawQuery("select * from "+helper.TABLE_DESEJO, null);
cursor.moveToFirst();
while(!cursor.isAfterLast()){
Desejo d = new Desejo();
d.setId(cursor.getInt(0));
d.setProduto(cursor.getString(1));
d.setCategoria(cursor.getString(2));
d.setPrecoMinimo(cursor.getDouble(3));
d.setPrecoMaximo(cursor.getDouble(4));
d.setLojas(cursor.getString(5));
lista.add(d);
cursor.moveToNext();
}
cursor.close();
return lista;
}
public final int proximoId(){
try {
int id = 0;
Cursor cursor = database.rawQuery("select max(id) from desejo", null);
cursor.moveToFirst();
id = cursor.getInt(0);
return id + 1;
} catch (Exception e) {
e.printStackTrace();
return 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment