Skip to content

Instantly share code, notes, and snippets.

@hirauchg
Last active May 19, 2019 14:16
Show Gist options
  • Save hirauchg/e56916dd2f54768c9bf272029aef28e5 to your computer and use it in GitHub Desktop.
Save hirauchg/e56916dd2f54768c9bf272029aef28e5 to your computer and use it in GitHub Desktop.
AnkoでSQLiteデータベースを実装する - Managerの作成 - 1
class SampleManager(ctx: Context) {
private val mDB = SampleDBHelper.getInstance(ctx)
fun getSampleModelList(): List<SampleModel> {
lateinit var sampleModelList: List<SampleModel>
mDB.use {
sampleModelList = select(SampleDBHelper.TABLE_SAMPLE).parseList(classParser())
}
return sampleModelList
}
fun insertSampleModel(name: String, age: Int) {
mDB.use {
insert(SampleDBHelper.TABLE_SAMPLE,
SampleDBHelper.CULM_TEXT to name,
SampleDBHelper.CULM_NUMBER to age)
}
}
fun updateSampleModel(id: Int, name: String, age: Int) {
mDB.use {
update(SampleDBHelper.TABLE_SAMPLE,
SampleDBHelper.CULM_TEXT to name,
SampleDBHelper.CULM_NUMBER to age)
.whereSimple(SampleDBHelper.CULM_ID + " = ?", id.toString()).exec()
}
}
fun deleteSampleModel(id: Int) {
mDB.use {
delete(SampleDBHelper.TABLE_SAMPLE, SampleDBHelper.CULM_ID + " = ?", arrayOf(id.toString()))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment