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
Requeststable, we have thenameandmessagefields, and we can directly create them in theonCreatemethod of our database helper, as shown:
db.createTable("Requests", true,
"id" to INTEGER + PRIMARY_KEY + UNIQUE,
"name" to TEXT,
"message" to TEXT)- For the
customerstable, we will be using a better coding practice by making a data class and using it to define the columns of thecustomerstable. Given here is the code for ourCustomerdata class:
data class Customer(val id: Int, val name: String, val phone_num: String...