I looked through so many tutorials and it's still not working for me. I have a database that looks like this: databaseImage Note that the actual row does not match up with the id. I would like to delete from the actual row rather than using the id.
I have the following function:
`fun deleteDBData(TABLE_NAME:String, rowID : Int) {
val db = this.writableDatabase
db.delete(TABLE_NAME, "$ID_COL=$rowID", null)
}`
This however, deletes using the number in the id column rather than the actual row. So with this function, if I tell it to delete rowID = 4, it will delete row 1 rather than row 4. How do I get sqlite to delete row 4?
CodePudding user response:
You are telling to delete the row that has the ID_COL = 4 To delete the 4th row you can run a query like
delete from tran_log where id in (select id from tran_log limit 1 offset 3);
CodePudding user response:
You can find the id
of the n
th row with LIMIT 1
and OFFSET n - 1
after the ORDER BY
clause which I assume should use the column id
to sort the rows:
fun deleteDBData(TABLE_NAME: String, n: Long) {
val db = this.writableDatabase
db.delete(
TABLE_NAME,
"$ID_COL = (SELECT $ID_COL FROM $TABLE_NAME ORDER BY $ID_COL LIMIT 1 OFFSET ? - 1)",
arrayOf(n.toString())
)
db.close()
}