Implementacion de BD con el login

This commit is contained in:
sebastiancc27 2024-04-20 19:50:24 -06:00
parent 1892d5d8f8
commit bc005717db
5 changed files with 111 additions and 6 deletions

View File

@ -12,9 +12,6 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.MedicalHealth" android:theme="@style/Theme.MedicalHealth"
tools:targetApi="31" > tools:targetApi="31" >
<activity
android:name=".IniciarSesionActivity"
android:exported="false" />
<activity <activity
android:name=".EliminarActivity" android:name=".EliminarActivity"
android:exported="false" /> android:exported="false" />

View File

@ -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)

View File

@ -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
}
}

View File

@ -1,11 +1,45 @@
package com.terratenientes.medicalhealth package com.terratenientes.medicalhealth
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import android.widget.Toast
import com.terratenientes.medicalhealth.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var db: DoctorDataBaseHelper
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) 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()
}
}
} }

View File

@ -30,6 +30,7 @@
android:textColorHint="@color/tv_titulo" android:textColorHint="@color/tv_titulo"
android:textSize="25sp" /> android:textSize="25sp" />
<EditText <EditText
android:id="@+id/tv_contrasena" android:id="@+id/tv_contrasena"
android:layout_marginTop="25dp" android:layout_marginTop="25dp"
@ -41,6 +42,8 @@
android:textColorHint="@color/tv_titulo" android:textColorHint="@color/tv_titulo"
android:textSize="25sp" android:textSize="25sp"
android:paddingStart="10dp" android:paddingStart="10dp"
android:ems="10"
android:inputType="textPassword"
/> />
<Button <Button
@ -65,6 +68,4 @@
/> />
</LinearLayout> </LinearLayout>