Skip to content

Instantly share code, notes, and snippets.

@alibahaaa
Created January 19, 2023 11:32
Embed
What would you like to do?
class DatabaseHelper private constructor(context: Context) {
companion object {
private var instance: DatabaseHelper? = null
fun getInstance(context: Context): DatabaseHelper {
if (instance == null) {
instance = DatabaseHelper(context)
}
return instance!!
}
}
private val database: SQLiteDatabase = context.openOrCreateDatabase("app_db", Context.MODE_PRIVATE, null)
fun executeQuery(query: String) {
database.execSQL(query)
}
fun getData(query: String): Cursor {
return database.rawQuery(query, null)
}
}
val databaseHelper = DatabaseHelper.getInstance(applicationContext)
val cursor = databaseHelper.getData("SELECT * FROM users")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment