Room database
For part 1 of room database tutorial visit and for video tutorial visit
For more info we have seen other local databases before may have a look.
Room database provides the easy to use interface using which we can perform all the CRUD operations.Room works over the SQLite database.
Having interface kind of approach will help us to make use of Kotlin coroutine and also avoid unwanted SQL query format errors at run time.
We have seen data insertion in previous blog now we will go through the remaining CRUD operations i.e., Read, Delete and Update.
Read Operation :
fragment_read_user.xml
Utilizing a scroll view instead of list view to make example compact in real time you may use recyclerlistview
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="ReadUserModel" type="com.abhi.roomdatabase.bindingModel.ReadUserModel" /> </data> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/userDetails" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" /> </ScrollView> </layout>
ReadUserFragment.java
Initialize data binding and fetch the saved user record from room database
public class ReadUserFragment extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { FragmentReadUserBinding fragmentReadUserBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_read_user, container, false); List<User> users = HomeActivity.myAppDatabase.myDao().getUsers(); String info = ""; for(User user: users){ int id = user.getId(); String name = user.getName(); String email = user.getEmail(); info = info +"id : "+ id+"\n"+ "name : " + name+" \n"; } fragmentReadUserBinding.userDetails.setText(info); return fragmentReadUserBinding.getRoot(); } }
Final view of the screen which show database read query.
Delete Operation :
fragment_delete_user.xml
Input field to accept id to be deleted.Id plays a key role in deletion so provide an alert for the user to confirm the action because the data assigned to the id will be deleted.
<?xml version="1.0" encoding="utf-8"?> <layout> <data> <variable name="DeleteUserModel" type="com.abhi.roomdatabase.bindingModel.DeleteUserModel" /> </data> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/deleteId" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="enter id to be delete" /> <Button android:id="@+id/deleteBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="delete" /> </LinearLayout> </layout>
DeleteUserFragment.java
Initialize data binding to fetch the user input from edittext and pass it to delete operation.
public class DeleteUserFragment extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { final FragmentDeleteUserBinding deleteUserBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_delete_user,container,false); deleteUserBinding.deleteBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { User user = new User(); if(deleteUserBinding.deleteId.getText().toString().length()>0) user.setId(Integer.parseInt(deleteUserBinding.deleteId.getText().toString())); HomeActivity.myAppDatabase.myDao().deletUser(user); } }); return deleteUserBinding.getRoot(); } }
Final view of the screen which show database delete query.
Update Operation :
fragment_update_user.xml
Add fields required to update user record and a button to update changes.
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="UpdateUserModel" type="com.abhi.roomdatabase.bindingModel.UpdateUserModel" /> </data> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:padding="20dp"> <EditText android:id="@+id/id" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="user id"/> <EditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="user name"/> <EditText android:id="@+id/email" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="user email"/> <Button android:id="@+id/btnSubmit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Update User"/> </LinearLayout> </layout>
UpdateUserFragment.java
Initialize the data binding to update previous data.Here we consider the id and we will provide the data which is updated and also remaining fields once again.
public class UpdateUserFragment extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { final FragmentUpdateUserBinding fragmentUpdateUserBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_update_user,container,false); fragmentUpdateUserBinding.btnSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int id = Integer.parseInt(fragmentUpdateUserBinding.id.getText().toString()); String name = fragmentUpdateUserBinding.name.getText().toString(); String email = fragmentUpdateUserBinding.email.getText().toString(); User user = new User(); user.setId(id); user.setName(name); user.setEmail(email); HomeActivity.myAppDatabase.myDao().updateUser(user); Toast.makeText(getContext(), "User updated successfully", Toast.LENGTH_SHORT).show(); } }); return fragmentUpdateUserBinding.getRoot(); } }
Output :
Final view of the screen showing room database update query.
For more on database tutorials may visit this link https://androidcoding.in/?s=database.
Any query on the tutorial on room database try to mention in the comment section, also if you found this tutorial interesting do share, like this tutorial.