Hi everyone, This is Vignesh from CodeAndroid In last tutorial we have created SQLite Database and we did the insertion In this video will see, how to read, delete and update the records. From the DB(Database) Add a LinearLayout below insert form’s layout to do the operation we need 3 buttons So setting “weightSum” as 3 adding a Button with width as “match_parent” and height as “wrap_content” assigning weight as “1” and setting id as “btn_read” setting text as “Read” select the code and press “ctrl+d” on your keyboard changing the id to “btn_update” and text as “update” changing the id to “btn_update” and text as “delete”. we have “weightSum” as 3 for the LinearLayout and inside buttons are having weight as 1 so all the buttons will get the same width to show the result adding a ScrollView width as “match_parent” and height as “match_parent” under the control Button layout. Inside keeping TextView and assigning it as “tvResult” open Database handler file creating a function readData which will return MutableList of “user” objects. crating the MutableList and returning the list we need to initialize the list with ArrayList here we are going to read the data from the DB(Database) so getting reableDatabase to get all the data we need to use “Select * from TableName” as query. if we execute the query using rawQuery we will get the cursor object this will have all the data’s read from the DB we need to close the cursor and Database using close() in between, we need to read all the data’s from the cursor. moveToFirst() will return “true” if the cursor is not null and has atleast one value. inside using an do..while loop we need to read the values. creating a user object inside the loop we don’t have the empty constructor so creating it. we have id as the 1st column But index will start from 0 so giving index as 0 for the id converting the string to Integer using toInt() similarly setting the name and age from index 1 and 2. if we don’t know the proper index we can get the index using a method getColumnIndex by passing the ColumnName Replacing the index with this method. will add the user object to the list using add(). open MainActivity.kt setting onClickListener for btn_read to call readData() we need DatabaseHandler object so moving this to the top level. using the handler object calling readData() setting the resulting text as empty first. Now looping the data using a for loop and appending the object into tvResult TextView in for loop we need to loop through 0 to (size -1) Let’s run the application. we are getting the data into the TextView. Adding spaces between user attributes. creating a function deleteDate getting writableDatabase and closing the DB connection. to delete the record we need to call delete() on the connection object here we need to pass 3 parameters TableName, whereclass, and values delete the row with “COL_ID” as 1 if we want to delete multiple records then we need to pass all the ID’s inside arrayOf() duplicating the readData() Remove the return type and list, also, get the writableDatabase On calling this method, we are going to increment the age by 1 so we need to read all the data, and instead of adding into the list we need to update the age db.update() inside we need to pass 4 parameters TableName, contentValues,whereClass and value[] creating the contentValues with age. and passing the cv here. here I am matching id and name. passing id and name values. setting onClickListener for btn_update and calling updateData() also programmatically clicking the btn_read Button Similarly setting onClickListener for btn_delete and calling deleteData() also programmatically clicking the btn_read Button Let’s Run the Code. Inserting a new row. clicking on “Read” the result is getting the data click on update nothing happening sorry, i forgot to increment the value Let’s Run the Code. you can see on click of update All the records age are incremented by 1 onClick of delete record with id as 1 is removed to delete all the records we need to pass null as whereClass and null as value[] Let’s Run the Code. on click of the delete button all the records are removed to verify clicking on “read” nothing happening because all records are removed from the DB That’s all for this tutorial guys If you like this video Please give a thumbs up If you have any doubts, Please put it in the comment section, I will try to answer it. Also If you need any tutorial on “Android” let me know in the comment section I will try to do that Hope you like it I will see you in my next video Have a “GOOD DAY”

Android Tutorial (Kotlin) – 31 – Read Delete and update SQlite Database Records
Tagged on:                                                                                         

8 thoughts on “Android Tutorial (Kotlin) – 31 – Read Delete and update SQlite Database Records

  • December 24, 2017 at 3:28 pm
    Permalink

    Thanks for d tuts..how to include prepopulated database in kotlin,same as in java? also can u pls do Realm in Kotlin..and how to display iit in RecyclerView

    Reply
  • December 25, 2017 at 9:44 am
    Permalink

    Have you something about Dialog Box?

    Reply
  • March 28, 2018 at 9:40 pm
    Permalink

    Source code link doesnt work brother. Great video though.

    Reply
  • September 9, 2018 at 2:41 am
    Permalink

    Thanks from Turkey!

    Reply
  • November 9, 2018 at 11:30 am
    Permalink

    It's not working
    Do i need storage permission?

    Reply
  • August 8, 2019 at 5:57 pm
    Permalink

    What is to do, if you have a nested Select or even a double nested Select – SQL- Statement ?

    Reply
  • March 11, 2020 at 1:34 am
    Permalink

    Add and retrieve image using sqlite plz

    Reply
  • March 14, 2020 at 11:57 am
    Permalink

    Thank you bro..it's really helpful

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *