· 7 years ago · Nov 24, 2018, 04:08 AM
1public class BaseDeDatos extends SQLiteOpenHelper {
2
3
4public BaseDeDatos(Context context) {
5 super(context, "petshotel.db", null, 8);
6}
7private static final String tabla_pets = "CREATE TABLE IF NOT EXISTS pets(id integer primary key autoincrement, nombre text, edad text, raza text, alergia text, reserva text)";
8private static final String tabla = "pets";
9private static final String tabla_cuidados = "CREATE TABLE IF NOT EXISTS cuidados(id integer primary key autoincrement, reserva text, dia text, turno text, baniar text, pasear text, cortar text, jugar text, comentario text)";
10
11
12
13@Override
14public void onCreate(SQLiteDatabase db) {
15 db.execSQL("create table reservas(id integer primary key autoincrement,usuario integer, nombre text, apellido text, correo text, telefono text, fecha_inicio text, fecha_fin text)");
16 db.execSQL("insert into reservas values(1, 1111, 'Rodrigo', 'Paz', 'rodropaz@gmail.com', '1134720205', '2018-01-01', '2018-01-07'), (2, 2222, 'Juan', 'Perez', 'juanperez@gmail.com', '1165478954', '2018-02-01', '2018-02-05')");
17 db.execSQL(tabla_pets);
18 db.execSQL(tabla_cuidados);
19}
20
21@Override
22public void onUpgrade(SQLiteDatabase db, int i, int i1) {
23 db.execSQL("DROP TABLE IF EXISTS reservas");
24 db.execSQL("DROP TABLE IF EXISTS pets");
25 db.execSQL("DROP TABLE IF EXISTS cuidados");
26 db.execSQL("create table reservas(id integer primary key autoincrement,usuario integer, nombre text, apellido text, correo text, telefono text, fecha_inicio text, fecha_fin text)");
27 db.execSQL("insert into reservas values(1, 1111, 'Rodrigo', 'Paz', 'rodropaz@gmail.com', '1134720205', '2018-01-01', '2018-01-07'), (2, 2222, 'Juan', 'Perez', 'juanperez@gmail.com', '1165478954', '2018-02-01', '2018-02-05')");
28 db.execSQL(tabla_pets);
29 db.execSQL(tabla_cuidados);
30}
31
32public void insertarPet(Integer id, String nombre, String edad, String raza, String alergia, String reserva) {
33 SQLiteDatabase bd = getWritableDatabase();
34 if(bd != null){
35 bd.execSQL("INSERT INTO pets VALUES(null, '"+nombre+"','"+edad+"','"+raza+"','"+alergia+"', '"+reserva+"')");
36 bd.close();
37 }
38}
39
40public void insertarCuidado(Integer id, String reserva, String dia, String turno, String baniar, String pasear, String cortar, String jugar, String comentario) {
41 SQLiteDatabase bd = getWritableDatabase();
42 if(bd != null){
43 bd.execSQL("INSERT INTO cuidados VALUES(null, '"+reserva+"','"+dia+"','"+turno+"','"+baniar+"','"+pasear+"','"+cortar+"','"+jugar+"','"+comentario+"')");
44 bd.close();
45 }
46}
47
48public List<Pets> mostrarPets(){
49 SQLiteDatabase db = getReadableDatabase();
50 Cursor cursor = db.rawQuery("SELECT * FROM pets", null);
51 List<Pets> pets= new ArrayList<>();
52 if(cursor.moveToFirst()){
53 do {
54 pets.add(new Pets(cursor.getString(0),cursor.getString(1), cursor.getString(2), cursor.getString(3),
55 cursor.getString(4), cursor.getString(5)));
56
57 }while (cursor.moveToNext());
58 }
59 return pets;
60}
61
62public void eliminarTodo(){
63 SQLiteDatabase db = getWritableDatabase();
64 db.execSQL("DELETE FROM pets;");
65 Log.d("Eliminar pet","Datos borrados");
66}
67
68public Reserva getReserva(String id) {
69
70 SQLiteDatabase db = this.getReadableDatabase();
71 Cursor cursor = db.rawQuery("SELECT * FROM RESERVAS WHERE id = ?", new String[]{ id });
72 Reserva reserva = new Reserva();
73
74
75 if (cursor != null && cursor.moveToFirst()) {
76 reserva.setId(String.valueOf(cursor.getInt(0)));
77 reserva.setFecha_inicio(cursor.getString(6));
78 reserva.setFecha_fin(cursor.getString(7));
79
80
81 cursor.close();
82 db.close();
83 }
84
85 return reserva;
86}
87
88public class ElegirCuidados extends AppCompatActivity {
89
90private String reser;
91String id, fecha_ini, fecha_fin;
92
93@Override
94protected void onCreate(Bundle savedInstanceState) {
95 super.onCreate(savedInstanceState);
96 setContentView(R.layout.elegir_cuidados);
97
98 Bundle extras = getIntent().getExtras();
99 reser = extras.getString("reserva");
100
101 Toast.makeText(getApplicationContext(), reser, Toast.LENGTH_SHORT).show();
102
103 if (extras != null) {
104
105 str_reserva = "2";
106 Reserva reserva;
107
108 BaseDeDatos bd = new BaseDeDatos(this);
109 reserva = bd.getReserva(str_reserva);
110
111 id = reserva.getId();
112 fecha_ini = reserva.getFecha_inicio();
113 fecha_fin = reserva.getFecha_fin();
114
115 Toast.makeText(getApplicationContext(), id, Toast.LENGTH_SHORT).show();
116
117 TextView tvDesde = findViewById(R.id.tvDesde);
118 tvDesde.setText("Desde: " + fecha_ini);
119 TextView tvHasta = findViewById(R.id.tvHasta);
120 tvHasta.setText("Hasta: " + fecha_fin);
121
122@Override
123public int getItemCount() {
124
125 return listaPets.size() ;
126 // return listaRegistro.size();
127}