Creating database tables
Now that you have learned how to add anko-sqlite dependencies to your project and how to use SQLite database in the first recipe, the next step is learning how to create database tables.
Getting ready
We'll be using Android Studio 3 for coding. Ensure that you have added anko-sqlite to your build.gradle
file and gone through the first recipe on how to use a SQLite database.
How to do it…
We will be creating two tables: Requests
and customers
:
- For the
Requests
table, we have thename
andmessage
fields, and we can directly create them in theonCreate
method of our database helper, as shown:
db.createTable("Requests", true, "id" to INTEGER + PRIMARY_KEY + UNIQUE, "name" to TEXT, "message" to TEXT)
- For the
customers
table, we will be using a better coding practice by making a data class and using it to define the columns of thecustomers
table. Given here is the code for ourCustomer
data class:
data class Customer(val id: Int, val name: String, val phone_num: String...