Created
June 26, 2018 10:28
-
-
Save nesk/11caa67d7027ce1391f6c23734b59633 to your computer and use it in GitHub Desktop.
Android pre-populated database (update methods)
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
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