Guest User

Untitled

a guest
Jun 24th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. package studio.com.projetofinal;
  2.  
  3. private static final String DATABASE_NAME = "bancodedadosfinal.db";
  4. private static final int DATABASE_VERSION = 1;
  5. private static final String TABLE_NAME = "cadastro";
  6.  
  7. private Context context;
  8. private SQLiteDatabase db;
  9.  
  10. private SQLiteStatement InsertStnt;
  11. private static final String Insert = "insert into " + TABLE_NAME + " (nome, cpf, idade, telefone, email) values (?, ?, ?, ?, ?)";
  12.  
  13.  
  14. public DBcadastro(Context context){
  15. this.context = context;
  16. Opencadastro opencadastro = new Opencadastro(this.context);
  17. this.db = opencadastro.getWritableDatabase();
  18. this.InsertStnt = this.db.compileStatement(Insert);
  19. }
  20.  
  21. public long insert (String nome, int cpf,int idade, int telefone, String email ){
  22. this.InsertStnt.bindString(1,nome);
  23. this.InsertStnt.bindLong(2,cpf);
  24. this.InsertStnt.bindLong(3,idade);
  25. this.InsertStnt.bindLong(4,telefone);
  26. this.InsertStnt.bindString(5,email);
  27. return this.InsertStnt.executeInsert();
  28. }
  29.  
  30. public void deleteAll (){
  31. this.db.delete(TABLE_NAME,null, null);
  32. }
  33.  
  34. public List<cadastro> queryGetALL(){
  35. List<cadastro> list = new ArrayList<cadastro>();
  36.  
  37. try {
  38.  
  39. Cursor cursor = this.db.query (TABLE_NAME, new String[] {"nome", "cpf","idade", "telefone", "email"},
  40. null, null, null, null, null);
  41.  
  42. int registros = cursor.getCount();
  43.  
  44. if (registros != 0){
  45. cursor.moveToFirst();
  46.  
  47. do {
  48. cadastro cadastro = new cadastro(cursor.getString(0), cursor.getInt(1), cursor.getInt(2),cursor.getInt(3),cursor.getString(4));
  49. list.add(cadastro);
  50. } while (cursor.moveToNext());
  51.  
  52. if (cursor != null && ! cursor.isClosed())
  53. cursor.close();
  54. return list;
  55. }else
  56. return null;
  57.  
  58. }
  59.  
  60. catch (Exception err) {
  61. return null;
  62. }
  63. }
  64.  
  65. private static class Opencadastro extends SQLiteOpenHelper{
  66. Opencadastro(Context context){
  67. super(context, TABLE_NAME, null, DATABASE_VERSION);
  68.  
  69. }
  70.  
  71. public void onCreate (SQLiteDatabase db){
  72. String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(id INTEGER PRIMARY KEY AUTOINCREMENT, nome TEXT " +
  73. "cpf TEXT, idade TEXT, telefone TEXT, email TEXT);";
  74. db.execSQL(sql);
  75. }
  76.  
  77. public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){
  78. db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  79. onCreate(db);
  80. }
  81. }
Add Comment
Please, Sign In to add comment