Using SQLite

The first thing to do is to define a schema of your database. The schema of a database is what defines how the data in the database is organized. It defines the tables into which the data is organized, and restrictions on those tables (such as allowed data types for the columns). It is advisable to create a contract class that specifies the details of your database.

Create a new Kotlin object, with the name TodoListDBContract, and replace its contents with the following lines of code:

object TodoListDBContract {

const val DATABASE_VERSION = 1
const val DATABASE_NAME = "todo_list_db"

class TodoListItem: BaseColumns {
companion object {
const val TABLE_NAME = "todo_list_item"
const val COLUMN_NAME_TASK = "task_details"
const val COLUMN_NAME_DEADLINE = "task_deadline"
const val COLUMN_NAME_COMPLETED = "task_completed"
}
}

}

In the preceding code, the TodoListItem class represents a table in our database, and is used to declare the name of the table and names of its columns.

To create a new Kotlin object, first, right-click the package and select New
| Kotlin File/ClassThen in the New Kotlin File/Class dialog, select Object in the Kind field:

The next thing to do is to create a database helper class. This will help us abstract the connection to the database and not keep the database connection logic in our Activity. Go ahead and create a new Kotlin class with the name TodoListDBHelper. The class should take a Context parameter in its default constructor and extend the android.database.sqlite.SQLiteOpenHelper class as shown in the following code:

class TodoListDBHelper(context: Context): SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {

Now, add the following lines of code to the TodoListDBHelper class as shown in the following code:

private val SQL_CREATE_ENTRIES = "CREATE TABLE " + TodoListDBContract.TodoListItem.TABLE_NAME + " (" +
BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
TodoListDBContract.TodoListItem.COLUMN_NAME_TASK + " TEXT, " +
TodoListDBContract.TodoListItem.COLUMN_NAME_DEADLINE + " TEXT, " +
TodoListDBContract.TodoListItem.COLUMN_NAME_COMPLETED + " INTEGER)" // 1


private val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + TodoListDBContract.TodoListItem.TABLE_NAME // 2

override fun onCreate(db: SQLiteDatabase) { // 3
db.execSQL(SQL_CREATE_ENTRIES)
}

override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {// 4
db.execSQL(SQL_DELETE_ENTRIES)
onCreate(db)
}

override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
onUpgrade(db, oldVersion, newVersion)
}

In the preceding lines of code the following applies:

  • SQL_CREATE_ENTRIES is a SQL query to create a table. It specifies an_id field, which is set as the primary key of the database.
In relational databases, a table is required to have a column that uniquely identifies each row entry. This unique column is known as the Primary Key. Specifying a column as AUTOINCREMENT tells the RDBMS to auto-generate a new value for this field whenever a new row is being inserted.
  • SQL_DELETE_ENTRIES is a SQL query to drop the table if it exists.
  • In the onCreate() method, the SQL query is executed to create the table.
  • In onUpgrade(), the table is deleted and recreated.

Since the table will have an ID field in the database, we have to add an extra field in the Task class to track it. Open Task.kt, add a new field of Long type, named taskId.

var taskId: Long? = null

Next, add the constructor shown as follows:

constructor(taskId:Long, taskDetails: String?, taskDeadline: String?, completed: Boolean) : this(taskDetails, taskDeadline) {
this.taskId = taskId
this.completed = completed
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset