Lecture 8
Lecture 8
Room
Author:Semen Naduiev
Introduction to SQLite
What is SQLite?
ACID-compliant.
Single-file database.
What is Room?
implementation "androidx.room:room-runtime:latest-version"
kapt "androidx.room:room-compiler:latest-version"
or add ksp
Basic Entity Definition
By default, Room uses the class name as the table name, but you can change it using tableName:
Primary Key in Room
A Primary Key is a unique identifier for each record in a database table. In Room, every Entity (table) must have at
least one field marked as a primary key.
A Unique Constraint ensures that a column (or a combination of columns) contains unique values across all rows in a table. This helps prevent duplicate entries in a database.
You can enforce uniqueness using the @Index annotation with unique = true:
If you try to insert a duplicate email, Room will throw an SQLite constraint exception.
Unique Constraints on Multiple Columns
A DAO (Data Access Object) is an interface that provides methods to interact with the database.
It allows you to insert, update, delete, and query data using annotations instead of writing raw
SQL queries.
Defining a DAO
A DAO is defined as an interface or an abstract class with methods annotated with @Insert,
@Update, @Delete, and @Query.
By default, @Insert throws an error if a duplicate entry is inserted. You can handle this using onConflict strategies:
@Insert(onConflict = OnConflictStrategy.REPLACE)
✔ IGNORE: If a duplicate exists, it will skip the insert and return -1.
Used to modify existing records. Room generates an UPDATE SQL statement based on the
primary key.
Delete (@Delete)
Allows you to execute custom SQL queries to read data from the database.
Transactions (@Transaction)
Unlike traditional SQL databases, SQLite is dynamically typed, meaning that it does not enforce
strict data types for columns. However, it does support five storage classes that define how data
is stored.
Querying Data
To retrieve data from a table, use the SELECT statement.
Joins in SQLite (Combining Tables)
In SQLite, joins allow you to combine rows from two or more tables based on a related column
between them. Joins are essential for querying data spread across multiple tables, ensuring that
relational data can be linked effectively.
1. INNER JOIN
2. LEFT JOIN (or LEFT OUTER JOIN)
3. CROSS JOIN
4. SELF JOIN
5. RIGHT JOIN (SQLite does not support this directly)
INNER JOIN
INNER JOIN returns only the rows where there is a match in both tables.
Example:
We have two tables, users and orders, and we want to get the list of users and their orders.
LEFT JOIN (LEFT OUTER JOIN)
LEFT JOIN returns all rows from the left table and matched rows from the right table. If there is
no match, the result is NULL from the right table.
Example:
In this case, we want to get all users, including those without orders.
CROSS JOIN
CROSS JOIN returns the Cartesian product of two tables. This means it returns all possible
combinations of rows from both tables, without any condition.
Example:
Assume you have two tables, colors and products, and you want to pair every product with each
color.
SELF JOIN
A SELF JOIN is a join where a table is joined with itself. It is often used to compare rows within
the same table.
Example:
Let’s assume we have a employees table, where employees report to other employees
(manager-subordinate relationship).
Setting up the Database Class
In Android development, Room provides an abstraction layer over SQLite to allow more robust
database access while leveraging SQLite’s full power. The Database class in Room is the central
point for your database configuration.
Define RoomDatabase Class
The Room Database class is where you define the database configuration and serve as the main
access point for the app's data.