![]() |
VOOZH | about |
We have seen How to Create and Add Data to SQLite Database in Android using Jetpack Compose as well as How to Read Data from SQLite Database in Android using Jetpack Compose. We have performed different SQL queries for reading and writing our data to SQLite database. In this article, we will take a look at updating data to SQLite database in Android using Jetpack Compose.
Refer to the previous articles to know about how to Create & Add Data and Read Data from SQLite Database.
We will be building a simple application in which we were already adding as well as reading the data. Now we will simply update our data in a new activity and we can get to see the updated data. A sample video is given at the end to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language.
To create a new project in the Android Studio, please refer to How to Create a new Project in Android Studio with Jetpack Compose.
In the previous articles mentioned above, we created a Database Handler Class named DBHandler.kt to handle out database operation. In this step, we will update this class by adding a function updateCourse() to edit our data in a list. The code for the CourseModal.kt is also for convenience.
Navigate to app > kotlin+java > {package-name}, Right click on it, New > Empty Views Activity, name it as UpdateCourse and uncheck the Generate Layout file option. Now add below code to it. Comments are added in the code to get to know in detail. In this file, it be almost as same as the MainActivity except for the text of a button. There will be only one button to update the course. In the next article, we will add a Delete button to delete a data from the database.
UpdateCourse.kt:
In this step, we will update the ViewCourses.kt file to add an onClick() to every card to open the UpdateCourse screen. We have already worked on this in the previous article mentioned above. This time we will just an onClick().
ViewCourses.kt:
There will be no changes made this time to the MainActivity.kt file. It is only provided for convenience.
MainActivity.kt: