
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();
- Android Error Unexpected cast to Button: layout tag was FrameLayout
- ADT quit unexpectedly error on Mac OSX Android Eclipse SDK
- Parsing Data for android-21 failed unsupported major.minor version 51.0
- Android Studio Ctrl Shift o auto import not working
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
- Android : How to make TextView Scrollable
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Integrating Android Facebook SDK 3.17.2 Tutorial
- Android R Cannot Be Resolved To A Variable
- Android : Exception raised during rendering: action_bar API 22
- How to take screenshot on Android
- Read Text file from SD Card : Android Programming
- How to make Android EditText not editable
- Your Android SDK is out of date or is missing templates. Please ensure you are using SDK version 22 or later.
- The declared package does not match the expected package Eclipse
- Can't Run SDK Manager find_java.bat issue
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- Android Emulator Soft Back button action using Computer keyboard
- Multiline EditText in Android Example
- Use 5G Network on Android Emulator
- Make Android TextView Clickable like Buttons
- How to empty trash in Android Device
- Android : Execute some code after back button is pressed
- Disable Fading Edges Scroll Effect Android Views
- How To Disable Landscape Mode in Android Application
- [fix] Docker: Alpine Linux - /bin/sh: bash: not found - Docker
- Disable jQuery button after being click - jQuery
- How to quit or exit SFTP Prompt on Terminal - Linux
- Remove items from JavaScript array - JavaScript
- Fix: Amazon Linux bash: sudo: command not found - AWS
- Create SharePoint Site Collection with new Content database in existing web application - SharePoint
- Twitch chat down, error loading data, content unavailable, streaming problem - News
- How to change the default location of Mac Screenshot - MacOS