Advertisement
Guest User

Untitled

a guest
Apr 10th, 2015
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.72 KB | None | 0 0
  1. package br.livro.android.cap14.banco;
  2.  
  3. import java.sql.SQLException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import br.livro.android.cap14.banco.Carro.Carros;
  8. import android.content.ContentValues;
  9. import android.content.Context;
  10. import android.database.Cursor;
  11. import android.database.sqlite.SQLiteDatabase;
  12. import android.database.sqlite.SQLiteQueryBuilder;
  13. import android.util.Log;
  14.  
  15. public class RepositorioCarro {
  16.     public static final String CATEGORIA = "livro";
  17.     private static final String NOME_BANCO = "livro_android";
  18.     public static final String NOME_TABELA = "carro";
  19.     protected SQLiteDatabase db;
  20.     public RepositorioCarro(Context ctx){
  21.         db = ctx.openOrCreateDatabase(NOME_BANCO, Context.MODE_PRIVATE, null);
  22.     }
  23.     protected RepositorioCarro(){}
  24.    
  25.     //Salva carro, insere m novo ou atualiza
  26.     public long salvar(Carro carro){
  27.         long id = carro.id;
  28.         if (id!=0){
  29.             atualizar(carro);
  30.         }else{
  31.             id = inserir(carro);
  32.         }
  33.         return id;
  34.     }
  35.    
  36.     //Insere um novo carro
  37.     public long inserir(Carro carro){
  38.         ContentValues values = new ContentValues();
  39.         values.put(Carros.NOME, carro.nome);
  40.         values.put(Carros.PLACA, carro.placa);
  41.         values.put(Carros.ANO, carro.ano);
  42.         long id = inserir(values);
  43.         return id;
  44.     }
  45.    
  46.     //Insere um novo Carro
  47.     public long inserir(ContentValues valores){
  48.         long id = db.insert(NOME_TABELA, "", valores);
  49.         return id;
  50.     }
  51.    
  52.     //Atualiza o banco, o id do carro é utilizado
  53.     public int atualizar(Carro carro){
  54.         ContentValues values = new ContentValues();
  55.         values.put(Carros.NOME, carro.nome);
  56.         values.put(Carros.PLACA, carro.placa);
  57.         values.put(Carros.ANO, carro.ano);
  58.         String _id = String.valueOf(carro.id);
  59.         String where = Carros._ID  + "=?";
  60.         String[] whereArgs = new String[] { _id};
  61.         int count = atualizar(values,where,whereArgs);
  62.         return count;
  63.     }
  64.    
  65.     //Atualiza o carro com os valores abaixo
  66.     //A clausula where é utilizada p/ identificar o carro a ser utilizado
  67.     public int atualizar(ContentValues valores, String where, String[] whereArgs){
  68.         int count = db.update(NOME_TABELA, valores, where, whereArgs);
  69.         Log.i(CATEGORIA, "Atualizou [" + count  +"] registros");
  70.         return count;
  71.     }
  72.    
  73.     //Deleta o carro com o id fornecido
  74.     public int deletar(long id){
  75.         String where = Carros._ID + "=?";
  76.         String _id = String.valueOf(id);
  77.         String[] whereArgs = new String[]{_id};
  78.         int count = deletar(where,whereArgs);
  79.         return count;
  80.     }
  81.    
  82.     //Deleta carro com os argumentos fornecidos
  83.     public int deletar(String where, String[] whereArgs){
  84.      int count = db.delete(NOME_TABELA, where, whereArgs);
  85.      Log.i(CATEGORIA, "Deletou[" + count + "] registros");
  86.      return count;
  87.     }
  88.    
  89.     //Busca o carro pelo id
  90.     public Carro buscarCarro(long id){
  91.         Cursor c = db.query(true, NOME_TABELA, Carro.colunas, Carros._ID  +"=" + id, null, null, null, null, null);
  92.         if (c.getCount()>0){
  93.             c.moveToFirst();
  94.             Carro carro = new Carro();
  95.             carro.id = c.getLong(0);
  96.             carro.nome = c.getString(1);
  97.             carro.placa = c.getString(2);
  98.             carro.ano = c.getInt(3);
  99.             return carro;
  100.         }
  101.         return null;
  102.     }
  103.    
  104.     //Retorna um cursor com todos os carros
  105.     public Cursor getCursor(){
  106.         return db.query(NOME_TABELA, Carro.colunas, null, null, null, null, null, null);
  107.     }
  108.    
  109.     public List<Carro> listarCarros(){
  110.         Cursor c = getCursor();
  111.         List<Carro> carros = new ArrayList<Carro>();
  112.         if (c.moveToFirst()){
  113.             int idxID = c.getColumnIndex(Carros._ID);
  114.             int idxNome = c.getColumnIndex(Carros.NOME);
  115.             int idxPlaca = c.getColumnIndex(Carros.PLACA);
  116.             int idxAno = c.getColumnIndex(Carros.ANO);
  117.             do{
  118.                 Carro carro = new Carro();
  119.                 carros.add(carro);
  120.                 carro.id = c.getLong(idxID);
  121.                 carro.nome = c.getString(idxNome);
  122.                 carro.placa = c.getString(idxPlaca);
  123.                 carro.ano = c.getInt(idxAno);
  124.             }while (c.moveToNext());
  125.         }
  126.         return carros;
  127.     }
  128.    
  129.     //Busca o carro pelo nome
  130.     public Carro buscarCarroPorNome(String nome){
  131.         Carro carro = null;
  132.         Cursor c = db.query(NOME_TABELA, Carro.colunas, Carros.NOME + " = '" + nome + "'", null, null, null, null);
  133.         if (c.moveToNext()){
  134.             carro = new Carro();
  135.             carro.id = c.getLong(0);
  136.             carro.nome = c.getString(1);
  137.             carro.placa = c.getString(2);
  138.             carro.ano = c.getInt(3);
  139.         }
  140.         return carro;
  141.     }
  142.    
  143.     //Busca um carro utilizabdo as configurações definidas
  144.     //no SQLiteBuolder
  145.     //Utilizado pelo Content Provider de carro
  146.     public Cursor query(SQLiteQueryBuilder queryBuilder, String[] projection, String selection, String[] selectionArgs,
  147.             String groupBy, String having, String orderBy){
  148.         Cursor c = queryBuilder.query(this.db, projection, selection, selectionArgs, groupBy, having, orderBy);
  149.         return c;
  150.        
  151.     }
  152.    
  153.     //Fecha o banco
  154.     public void fechar(){
  155.         if (db!=null){
  156.             db.close();
  157.         }
  158.     }
  159.    
  160.    
  161.    
  162.    
  163.        
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement