how can I create a login in koltin using sqlite.
User register
bt_204.setOnClickListener {
val admin = AdminSQliteOpenHelper(this, "administracion",null,1)
val bd = admin.writableDatabase
val registro = ContentValues()
registro.put("nombre_usuario", et_nombre_201.text.toString())
registro.put("clave1", et_pasword_204.text.toString())
registro.put("clave2",et_paword_203.text.toString() )
bd.insert("usuario", null , registro)
bd.close()
et_nombre_201.setText("")
et_paword_203.setText("")
et_pasword_204.setText("")
Toast.makeText(this,"Usuarios registrado correctamente", Toast.LENGTH_SHORT).show()
val intent = Intent (this,MainActivity::class.java)
startActivity(intent)
}
main activity
try this but the app is destroyed(I am a beginner)
bt_ingresar_04.setOnClickListener {
val admin = AdminSQliteOpenHelper(this, "administracion", null, 1)
val bd = admin.writableDatabase
val fila = bd.rawQuery(
"select nombre_usuario,clave1 from usuario where nombre_usuario=${et_02.text} && ${etp_03.text}"
null
)
if (fila.moveToFirst()) {
val intent = Intent(this, MainActivity_03::class.java)
startActivity(intent)
} else {
Toast.makeText(this, "Usuario no registrado", Toast.LENGTH_SHORT).show()
}
}
&&
is the logical operator for Java but not for sql. You must use AND
.
Also in the where
clause of the query you must specify all the needed column names, but you don't. You specify only the column nombre_usuario
.
Also the parameters et_02.text
and etp_03.text
should be properly quoted, so the final result is something like:
where column1 = 'value1' and column2 = 'value2'
But the proper way of doing this is by using ?
placholders and pass the parameters in the 2nd argument of rawQuery()
as an array of strings.
So change to this:
val fila = bd.rawQuery(
"select nombre_usuario, clave1 from usuario where nombre_usuario = ? and column_password = ?",
arrayOf(et_02.text, etp_03.text)
)
Replace column_password
with the name of the column where you store the password.
Also, in bt_204.setOnClickListener
, in this line:
registro.put("clave2",et_paword_203.text.toString() )
maybe there is a typo for et_paword_203
.