From bc005717db794eed465729541482c28825e0b7ed Mon Sep 17 00:00:00 2001 From: sebastiancc27 Date: Sat, 20 Apr 2024 19:50:24 -0600 Subject: [PATCH] Implementacion de BD con el login --- app/src/main/AndroidManifest.xml | 3 - .../terratenientes/medicalhealth/Doctor.kt | 3 + .../medicalhealth/DoctorDataBaseHelper.kt | 70 +++++++++++++++++++ .../medicalhealth/MainActivity.kt | 36 +++++++++- app/src/main/res/layout/activity_main.xml | 5 +- 5 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/com/terratenientes/medicalhealth/Doctor.kt create mode 100644 app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2b1a22d..e1e6e71 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,9 +12,6 @@ android:supportsRtl="true" android:theme="@style/Theme.MedicalHealth" tools:targetApi="31" > - diff --git a/app/src/main/java/com/terratenientes/medicalhealth/Doctor.kt b/app/src/main/java/com/terratenientes/medicalhealth/Doctor.kt new file mode 100644 index 0000000..7d7beb8 --- /dev/null +++ b/app/src/main/java/com/terratenientes/medicalhealth/Doctor.kt @@ -0,0 +1,3 @@ +package com.terratenientes.medicalhealth + +data class Doctor(var Cedula : String ,var Nombre : String, var Apellido : String, var Telefono : String, var Consultorio : Int) diff --git a/app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt b/app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt new file mode 100644 index 0000000..62af9c8 --- /dev/null +++ b/app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt @@ -0,0 +1,70 @@ +package com.terratenientes.medicalhealth + +import android.annotation.SuppressLint +import android.content.ContentValues +import android.content.Context +import android.database.sqlite.SQLiteDatabase +import android.database.sqlite.SQLiteOpenHelper +import android.widget.Toast + +class DoctorDataBaseHelper (context: Context) : SQLiteOpenHelper(context, DATABASE_NAME,null, + DATABASE_VERSION){ + companion object{ + private const val DATABASE_NAME ="MedicalHealth" + private const val DATABASE_VERSION=1 + private const val TABLE_NAME="Doctores" + private const val COLUMN_CEDULA="Cedula" + private const val COLUMN_NOMBRE="Nombre" + private const val COLUMN_APELLIDO="Apellido" + private const val COLUMN_TELEFONO="Telefono" + private const val COLUMN_CONSULTORIO="Consultorio" + private const val COLUMN_CONTRASENA="Contrasena" + } + + + @SuppressLint("SuspiciousIndentation") + override fun onCreate(db: SQLiteDatabase?) { + val crearTablaDoctores="CREATE TABLE $TABLE_NAME($COLUMN_CEDULA VARCHAR(30) PRIMARY KEY, $COLUMN_NOMBRE VARCHAR(30)," + + "$COLUMN_APELLIDO VARCHAR(30), $COLUMN_TELEFONO VARCHAR(30), $COLUMN_CONSULTORIO INTEGER, $COLUMN_CONTRASENA VARCHAR(30));" + db?.execSQL(crearTablaDoctores) + } + + override fun onUpgrade(db: SQLiteDatabase?, p1: Int, p2: Int) { + val dropTableDoctores = "DROP TABLE IF EXISTS $TABLE_NAME" + db?.execSQL(dropTableDoctores) + } + fun agregarDoctores(doctor : Doctor, contrasena: String){ + val db=writableDatabase + val values = ContentValues().apply { + put(COLUMN_CEDULA,doctor.Cedula) + put(COLUMN_NOMBRE, doctor.Nombre) + put(COLUMN_APELLIDO, doctor.Apellido) + put(COLUMN_TELEFONO, doctor.Telefono) + put(COLUMN_CONSULTORIO, doctor.Consultorio) + put(COLUMN_CONTRASENA, contrasena) + } + db.insert(TABLE_NAME,null, values) + db.close() + } + fun createTable(){ + val db=writableDatabase + val crearTablaDoctores="CREATE TABLE $TABLE_NAME($COLUMN_CEDULA VARCHAR(30) PRIMARY KEY, $COLUMN_NOMBRE VARCHAR(30)," + + "$COLUMN_APELLIDO VARCHAR(30), $COLUMN_TELEFONO VARCHAR(30), $COLUMN_CONSULTORIO INTEGER, $COLUMN_CONTRASENA VARCHAR(30));" + db?.execSQL(crearTablaDoctores) + } + fun dropTable(){ + val db=writableDatabase + val dropQuery="DROP TABLE IF EXISTS $TABLE_NAME" + db?.execSQL(dropQuery) + } + fun validarDatos(cedula :String , contrasena : String): Boolean{ + val db = readableDatabase + val validarQuery = "SELECT * FROM $TABLE_NAME WHERE $COLUMN_CEDULA = '$cedula' AND $COLUMN_CONTRASENA = '$contrasena'" + val cursor = db.rawQuery(validarQuery, null) + val isValid = cursor.count > 0 + cursor.close() + return isValid + } + + +} \ No newline at end of file diff --git a/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt b/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt index ef782d1..8323827 100644 --- a/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt +++ b/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt @@ -1,11 +1,45 @@ package com.terratenientes.medicalhealth +import android.annotation.SuppressLint import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.widget.Toast +import com.terratenientes.medicalhealth.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { + private lateinit var binding: ActivityMainBinding + private lateinit var db: DoctorDataBaseHelper override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_main) + binding=ActivityMainBinding.inflate(layoutInflater) + val view=binding.root + setContentView(view) + + db =DoctorDataBaseHelper(this@MainActivity) + binding.btnIniciarSesion.setOnClickListener { + var res=db.validarDatos(binding.tvUsuario.text.toString(),binding.tvContrasena.text.toString()) + if(res==true){ + Toast.makeText(this@MainActivity,"VALIDADO",Toast.LENGTH_LONG).show() + }else{ + Toast.makeText(this@MainActivity," NO VALIDADO",Toast.LENGTH_LONG).show() + } + } } + @SuppressLint("SuspiciousIndentation") + private fun cargarDatos(){ + val doctor= Doctor("1236","Sebastian","Cordoba","229330123",1) + db.agregarDoctores(doctor,"prueba") + Toast.makeText(this@MainActivity,"DATOS DEL DOCTOR AGREGADOS",Toast.LENGTH_LONG).show() + } + private fun validaDatos(){ + var usuario=binding.tvUsuario.text.toString() + var contrasena=binding.tvContrasena.text.toString() + var res=db.validarDatos(usuario,contrasena) + if(res==true){ + Toast.makeText(this@MainActivity,"VALIDADO",Toast.LENGTH_LONG).show() + }else{ + Toast.makeText(this@MainActivity," NO VALIDADO",Toast.LENGTH_LONG).show() + } + } + } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 9c365cc..aa5a1db 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -30,6 +30,7 @@ android:textColorHint="@color/tv_titulo" android:textSize="25sp" /> +