例如像
fun getOneName(name: String): Contact? {
val db = this.writableDatabase
val selectQuery = "SELECT * FROM $TABLE_NAME WHERE $colName = ?"
db.rawQuery(selectQuery, arrayOf(name)).use { // .use requires API 16
if (it.moveToFirst()) {
val result = Contact()
result.id = it.getInt(it.getColumnIndex(colId))
result.name = it.getString(it.getColumnIndex(colName))
return result
}
}
return null
}
像这样使用
btnGetID.setOnClickListener {
val dbManager = DBHelper(this)
val name = etPerson.text.toString()
val contact = dbManager.getOneName(name)
println("##################### where is return"+contact?.name)
}
$TABLE_NAME
和
$colName
"SELECT * FROM $TABLE_NAME WHERE $colName = ?"
"SELECT * FROM persons WHERE name = ?"
.
那么
db.rawQuery
运行,第二个参数用于提供实际的查询参数。
selectionArgs
不要这样做
"SELECT * FROM $TABLE_NAME WHERE $colName = '$name'"
或
little Bobby