The Code B define a Log table, I hope to clear all records except latest 10 records.
At present, I list all records order by CreatedDate first, then I do a loop from 11th record to last record, and delete the record using Code A.
Is there a better way to do that in Kotlin with anko ?
Code A
fun deleteDBLogByID(_id:Long)=mDBLogHelper.use{
delete(DBLogTable.TableNAME,"$idName = {$idName} ","$idName" to _id.toString() )
}
Code B
class DBLogHelper(mContext: Context = UIApp.instance) : ManagedSQLiteOpenHelper(
mContext,
DB_NAME,
null,
DB_VERSION) {
companion object {
val DB_NAME = "log.db"
val DB_VERSION = 1
val instance by lazy { DBLogHelper() }
}
override fun onCreate(db: SQLiteDatabase) {
db.createTable( DBLogTable.TableNAME , true,
DBLogTable._ID to INTEGER + PRIMARY_KEY+ AUTOINCREMENT,
DBLogTable.CreatedDate to INTEGER,
DBLogTable.Status to INTEGER +DEFAULT("0"),
DBLogTable.Description to TEXT
)
}
}