Skip to content

Instantly share code, notes, and snippets.

@alibahaaa
Created January 19, 2023 11:32
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 alibahaaa/40d4f2a5d53dda22e8bc5d93bf848114 to your computer and use it in GitHub Desktop.
Save alibahaaa/40d4f2a5d53dda22e8bc5d93bf848114 to your computer and use it in GitHub Desktop.
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