Advertisement
taniamg

BD

Mar 25th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. package estg.ipvc.sqliteandroid;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. public class BD extends SQLiteOpenHelper {
  13.  
  14. private static final int VERSAO_BANCO = 1;
  15. private static final String BANCO_CLIENTE = "bd_clientes";
  16.  
  17. private static final String TABELA_CLIENTE = "tb_clientes";
  18.  
  19. private static final String COLUNA_CODIGO = "codigo";
  20. private static final String COLUNA_NOME = "nome";
  21. private static final String COLUNA_TELEFONE = "telefone";
  22. private static final String COLUNA_EMAIL = "email";
  23.  
  24. public BD(Context context) {
  25. super(context, BANCO_CLIENTE, null, VERSAO_BANCO);
  26. }
  27.  
  28. @Override
  29. public void onCreate(SQLiteDatabase db) {
  30.  
  31. String QUERY_COLUNA = "CREATE TABLE " + TABELA_CLIENTE + "("
  32. + COLUNA_CODIGO + " INTEGER PRIMARY KEY, " + COLUNA_NOME + " TEXT, "
  33. +COLUNA_TELEFONE + " TEXT, " + COLUNA_EMAIL + " TEXT)";
  34.  
  35. db.execSQL(QUERY_COLUNA);
  36. }
  37.  
  38. @Override
  39. public void onUpgrade(SQLiteDatabase db, int i, int i1) {
  40.  
  41. }
  42.  
  43. /* ADICIONAR CLIENTE */
  44.  
  45. void addCliente(Cliente cliente) {
  46.  
  47. SQLiteDatabase db = this.getWritableDatabase();
  48.  
  49. ContentValues values = new ContentValues();
  50.  
  51. values.put(COLUNA_NOME, cliente.getNome());
  52. values.put(COLUNA_TELEFONE, cliente.getTelefone());
  53. values.put(COLUNA_EMAIL, cliente.getEmail());
  54.  
  55. db.insert(TABELA_CLIENTE, null, values);
  56. db.close();
  57. }
  58.  
  59. /* REMOVER CLIENTE */
  60.  
  61. void apagarCliente(Cliente cliente) {
  62.  
  63. SQLiteDatabase db = this.getWritableDatabase();
  64.  
  65. db.delete(TABELA_CLIENTE, COLUNA_CODIGO + " = ?", new String[] { String.valueOf(cliente.getCodigo())});
  66.  
  67. db.close();
  68. }
  69.  
  70. Cliente selecionarCliente(int codigo) {
  71. SQLiteDatabase db = this.getReadableDatabase();
  72.  
  73. Cursor cursor = db.query(TABELA_CLIENTE, new String[] {COLUNA_CODIGO,
  74. COLUNA_NOME, COLUNA_TELEFONE, COLUNA_EMAIL}, COLUNA_CODIGO + " = ?",
  75. new String[] {String.valueOf(codigo)}, null, null, null, null);
  76.  
  77. if(cursor != null) {
  78. cursor.moveToFirst();
  79. }
  80.  
  81. Cliente cliente = new Cliente(Integer.parseInt(cursor.getString(0)),
  82. cursor.getString(1),cursor.getString(2), cursor.getString(3));
  83.  
  84. return cliente;
  85. }
  86.  
  87. void atualizarCliente(Cliente cliente) {
  88.  
  89. SQLiteDatabase db = this.getWritableDatabase();
  90.  
  91. ContentValues values = new ContentValues();
  92. values.put(COLUNA_NOME, cliente.getNome());
  93. values.put(COLUNA_TELEFONE, cliente.getTelefone());
  94. values.put(COLUNA_EMAIL, cliente.getEmail());
  95.  
  96. db.update(TABELA_CLIENTE, values, COLUNA_CODIGO + " = ?",
  97. new String[] { String.valueOf(cliente.getCodigo()) });
  98. }
  99.  
  100. public List<Cliente> listaTodosClientes() {
  101. List<Cliente> listaClientes = new ArrayList<>();
  102.  
  103. String query = "SELECT * FROM " + TABELA_CLIENTE;
  104.  
  105. SQLiteDatabase db = this.getWritableDatabase();
  106. Cursor c = db.rawQuery(query, null);
  107.  
  108. if(c.moveToFirst()) {
  109. do {
  110. Cliente cliente = new Cliente();
  111. cliente.setCodigo(Integer.parseInt(c.getString(0)));
  112. cliente.setNome(c.getString(1));
  113. cliente.setTelefone(c.getString(2));
  114. cliente.setEmail(c.getString(3));
  115.  
  116. listaClientes.add(cliente);
  117. } while (c.moveToNext());
  118. }
  119.  
  120. return listaClientes;
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement