Skip to content

Instantly share code, notes, and snippets.

@nesk
Created June 26, 2018 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesk/11caa67d7027ce1391f6c23734b59633 to your computer and use it in GitHub Desktop.
Save nesk/11caa67d7027ce1391f6c23734b59633 to your computer and use it in GitHub Desktop.
Android pre-populated database (update methods)
package com.example.example
// ...
import android.content.SharedPreferences
class ActsDbHelper(val context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
private val preferences: SharedPreferences = context.getSharedPreferences(
"${context.packageName}.database_versions",
Context.MODE_PRIVATE
)
private fun installedDatabaseIsOutdated(): Boolean {
return preferences.getInt(DATABASE_NAME, 0) < DATABASE_VERSION
}
private fun writeDatabaseVersionInPreferences() {
preferences.edit().apply {
putInt(DATABASE_NAME, DATABASE_VERSION)
apply()
}
}
private fun installDatabaseFromAssets() {
// ...
}
override fun onCreate(db: SQLiteDatabase?) {
// Nothing to do
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
// Nothing to do
}
companion object {
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment