SQLiteOpenHelper is a helper class in Android that helps manage database creation and version management. It's used to create, open, and manage a database in your Android application.
Here's a breakdown:
About:
This class helps in managing database creation and version management, abstracting away the complexities of interacting with SQLite databases on Android.
What Use:
It's commonly used when you need to create or manage a local SQLite database within an Android app, handling tasks like creating the database if it doesn't exist, upgrading it when the schema changes, and providing a connection to the database.
Kotlin Syntax:
Using
SQLiteOpenHelper in Kotlin is quite similar to using it in Java. You'll extend the SQLiteOpenHelper class and override its methods to handle database creation and version management. Kotlin's concise syntax might make the code more concise and readable.
Example:
Here's a basic example of how you might use
SQLiteOpenHelper in Kotlin:import android.content.Context import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper class DBHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { companion object { private const val DATABASE_NAME = "MyDatabase" private const val DATABASE_VERSION = 1 private const val TABLE_NAME = "MyTable" private const val COLUMN_ID = "_id" // Add more column names and queries as needed } override fun onCreate(db: SQLiteDatabase?) { // Create your tables here val createTableQuery = "CREATE TABLE $TABLE_NAME ($COLUMN_ID INTEGER PRIMARY KEY)" db?.execSQL(createTableQuery) } override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { // Handle upgrades, e.g., alter table, drop table, etc. // db?.execSQL("DROP TABLE IF EXISTS $TABLE_NAME") //onCreate(db) } // Add methods to perform database operations as required fun insert(Double num1, Double num2, String operator, String answer) { val Qry = "INSERT INTO HISTORY(DATE,OPERAND1 , OPERATOR , OPERAND2 , ANSWER) VALUES" +"( datetime('now')," + num1 + ",'" + operator + "'," + num2 + ",'" + answer + "')" getWritableDatabase().execSQL(Qry) } fun selectData() : Cursor { val Qry = "select * from history Order by rowid desc" val cur : Cursor = getReadableDatabase().rawQuery(Qry,null) return cur } }
Attributes:
context: Context of the application or activity.
DATABASE_NAME: Name of the database.
DATABASE_VERSION: Database version. Increment this when you change the database schema.
onCreate(): Called when the database is created for the first time.
onUpgrade(): Called when the database needs to be upgraded due to a version change.
This class helps in handling the database operations efficiently and following good practices for SQLite database management within Android apps.
How to use cursor
val cur : Cursor = mydbhelp.selectData() while (cur.moveToNext()) { String opt = "At " + cur.getString(0) + "\n\n" + cur.getDouble(1) + " " + cur.getString(2) + " " + cur.getDouble(3) + " = " + cur.getString(4) + "\n"; data.add(opt) }