We can not believe we are asking this question!
How do we query a SQLite database for one record?
Say we want the id but only know the name in table
Here is the code that makes the call to the DB
btnGetID.setOnClickListener {
val dbManager = DBHelper(this)
val name = etPerson.text.toString()
dbManager.getOneName(name)
println("##################### where is return"+empName)
}
And here is the DB fun getOneName
fun getOneName(name: String): String {
val db = this.writableDatabase
val selectQuery = "SELECT * FROM $TABLE_NAME WHERE $colName = name"
// val selectQuery = "SELECT * FROM $TABLE_NAME WHERE $colId = id"
val cursor = db.rawQuery(selectQuery, null)
var empName = ""
//var empID = 0
if (cursor.getCount() > 0) {
cursor.moveToFirst()
empName = cursor.getString(cursor.getColumnIndex(colName))
//empID = cursor.getInt(cursor.getColumnIndex(colId))
}
cursor.close()
println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ID "+empName)
return empName
}
This DB has a Model and the app has an Adapter
class Contact{
var id: Int = 0
var name: String = ""
}
We would like to enter the name and retrieve the id
We can NOT even retrieve the name.
We have done this multiple times in Java but no luck with Kotlin