
Room in Android App development is a persistence library (just like an ORM tool JPA/Hibernate) that provides an abstraction layer over SQLite Database, thus this give you a benefit of an easy seamless database access without compromising the the full power of SQLite. It is recommended to make use of the Room instead of directly using SQLite because of the below reasons,
- Do compile time verification of your SQL queries.
- Streamline the migration path of your database.
- Make use of annotations that help to reduce boilerplate code and thus errors resulting from it.
- Easy seamless database access without compromising the the full power of SQLite
Tutorial: Setting up Room in Android Project
- Setting up Gradle: Add the dependency in build.gradle,
dependencies { .... implementation "androidx.room:room-runtime:2.4.1" annotationProcessor "androidx.room:room-compiler:2.4.1" .... }
- Create the Entity classes,
@Entity public class Employee { @PrimaryKey public int emp_id; @ColumnInfo(name = "emp_name") public String empName; @ColumnInfo(name = "emp_dept") public String empDept; @ColumnInfo(name = "emp_sal") public Double empSal; }
- Creating the DAO layer,
@Dao public interface EmployeeDao { @Query("SELECT * FROM employee") List< Employee > getAll(); @Query("SELECT * FROM employee WHERE emp_id IN (:empIds)") List<Employee> loadAllByEmpIds(int[] empIds); @Query("SELECT * FROM employee WHERE emp_name LIKE :empName LIMIT 1") Employee findByEmpName(String firstName); @Insert void insertAll(Employee... employee); @Delete void delete(Employee employee); }
Create the app database: You must extend RoomDatabase and annotate the class with @Database
@Database(entities = {Employee.class}, version = 1) public abstract class AppDatabase extends RoomDatabase { public abstract EmployeeDao employeeDao(); }
How to use The Room Database in your Android Application
Now that we have set the DAO, DAO implementation and the Database, lets use it in your Intents,
AppDatabase appDb = Room.databaseBuilder(getApplicationContext(),
AppDatabase.class, "database-name").build();
EmployeeDao empDao = db.empDao();
Employee empDetails = findByEmpName("Sam");
List<Employee> employees = empDao.getAll();
More Posts related to Android,
- Check Internet Connection WIFI 4G is active on Android Programmatically
- Android Emulator cannot be opened because the developer cannot be verified. [M1 Mac]
- How to Change Android Toast Position?
- Fail to connect to camera service Android java RuntimeException
- How to create Custom RatingBar Android Programming Tutorial
- Fixing Android unknown error 961 while downloading app
- Android AlertDialog with Yes No and Cancel Button
- Share or Send SMS via Android Intent
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- Pass data between two Android Activities and access it using Intent
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- Android EditText Cursor Colour appears to be white
- Android Development - How to switch between two Activities
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Android : Remove ListView Separator/divider programmatically or using xml property
- Android is starting optimizing... app 1 of 1
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- AlertDialog with single button example : Android
- Android : Exception raised during rendering: action_bar API 22
- Maven : java.lang.ClassNotFoundException: Xmx512m
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Center align text in TextView Android Programming
- How to Download and Install Android adb Tool on Linux, Mac or Windows
- Multiline EditText in Android Example
More Posts:
- Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED - Android
- Show CPU and Memory Usage on macOS Menu Bar - MacOS
- AWS S3 CLI BucketAlreadyExists when calling CreateBucket Error make_bucket failed - AWS
- Bash Command to Get Absolute Path for a File - Bash
- How to list all tables using Java JDBC - Java
- PHP Code for sending Emails - PHP
- How to add animated Gif to SharePoint Online Page - SharePoint
- 3 Ways to Change Default 8080 Port in Spring Boot - Java