Last active
May 19, 2019 14:16
-
-
Save hirauchg/e56916dd2f54768c9bf272029aef28e5 to your computer and use it in GitHub Desktop.
AnkoでSQLiteデータベースを実装する - Managerの作成 - 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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