How To Fetch Data From SQLite Database in Android Studio | tutorial-6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys welcome to fine gap youtube channel and in today's video we are going to learn how we can retrieve data from sqlite in android studio so let's start and before start please i request you to subscribe to our youtube channel to stay up to date with amazing programming videos so first of all we will create a model class so i will right click here click on new and click on java class i am going to name this model contact press enter button and inside this class i have to declare my three variables so the first variable that we want to the first column that we want to receive from our database is here i can show you my table these are my table fields id name and phone number so i will create three fields inside my model class so open the module context.java class and i will type here end id semicolon and this second data type will be string and here i'm going to write name and phone number semicolon you can also use getters and setters but i will not use in this lecture now again open my db helper class and inside this class i will create a function of type public and the this function type this function will return a array list and the type of this array list is exactly same our model contact class because we have to store our models of data inside this class so i will type here model contact and i am going to name this function add contact that's it and inside this function first of all we will get reference of our database to get the reference i have to type here sqlite database db is equal to this dot get readable database so because we want to retrieve data from our database that's why i will use get readable database function and next by using this database reference we have to use a function ra qd and inside this raw qd function we have to pass two parameters the first one will be our query and the second one will be the restriction of data suppose you want to receive data of only specific names that you can give condition in the second parameter but in this case i want to retrieve all the data from my database so i will pass null in my inside my second parameter and inside my first parameter i will type my query which is select static from space so static means all columns if you want to retrieve data of specific columns then i will write right here my column name suppose name and if you want to retrieve multiple columns data then i will type comma and then you can type here your second column name but in my case i want to retrieve all data from the database that's why i will use a static symbol here select all form and by concatenate this i have to type my database name sorry my table name so my table name is stored in a variable a table underscore name so i will just type my table name here table underscore name so this is my query and press comma and we have to pass selection arguments as null and press semicolon at the end and lastly we have to store this query in a cursor because using cursor we can read data from our database that's why i have to store this query inside the cursor next we have to initialize an array here arraylist and the type of this arraylist will be model contact and i'm going to name this array arraylist is equal to new arraylist so in this array we will store our module classes now i will use a while loop here and by using while loop because our data is stored in a cursor so from cursor we will store our data inside our model class so i will type here cursor dot move to next so this function will return false when your cursor will return all the rows so that's why we have to use move to next function and at next inside our while loop i will retrieve data from my columns and store that data inside our module class and then we will store those model classes inside the array that we have just created and now we will create an object of our model class so i will type here model contacts and i am going to name this object module contact is equal to new model contact so we just have create an object of our module contact class and now okay guys now we will retrieve data from our columns and we will uh set that data to our model class so you can use getters etc for that purpose or you can use constructor for this purpose or i will simply just set data to my variables of my model class by only using this object name which is model contact dot id is equal to so this variable exists inside my model contact class you can see at this variable and next by using the our cursor i have to type here cursor dot get int so the reason i use get it function because we have to store int value inside my id variable the value stored in that column if that value will be a string then we will use get string function so because id is an integer that's why i use here get int and inside this method we have to pass 0 semicolon guys because the reason i pass 0 here this is index of our table so let me show you the first column is id this column index will be zero and the second column is name then the second index will be name one and the third column name is phone underscore number so the third column will be the third columns index will be 2 so i will type here 0 because my index of our first column is 0 that's why i have passed 0 and second i have to again type my model contact dot name is equal to i will use cursor dot get string because this type we have to store a string value inside our name variable that's why i have used get string function here and i have to type here the index of my second column which is one semicolon and next i have to type here model contact dot phone number is equal to cursor dot get string and two so guys i hope you understand if you have any question or any confusion in this lecture you can just simply leave your comment and we will try to answer your question so guys inside our loop i have add my values inside our model and now we just have to add our model to our array that we have created the name of my array is array list so i will type here arraylist dot add and inside this i have to type my model name which is model contact so i just store retrieve values from my table and store those values in a model and then i just add that model in my arraylist so suppose uh if this while loop will execute 10 times then 10 model will be created and these model will be added to our array list line by line so that's it we have a store our model in our arraylist and finally we have to return our this array so i will use return word and array list our code of this function have been completed and because suppose i want to retrieve data from this function inside my main activity that's why i will type my code here inside my main activity to retrieve data from our database now i will comment by this insertion code and i will type code here to retrieve data from my database so i will use the reference of my database class which is dbhelper dot sorry guys we have used a wrong function name here add contact we have to change this to fetch contact because we are fetching contact by using this function we are not adding contact by using this function so i have changed this just change my this function name and inside our model contact uh inside our main activity i will type here patch contact function semicolon and this fetch contact function will return as an array of contact models and we have to store this in an arraylist and the type of this arraylist will be model contact and i'm going to name this data is equal to so that's it guys i will retrieve data from my database and i will show you inside my local let's just you use for loop i is equal to zero semicolon we have to use end i is equal to zero in i is the data type of our variable and i is less than data dot size semicolon and i plus plus semicolon and because we have to write only one line that's why you don't need to use mid brackets so guys now i have to remove this semicolon from here and we have to type here log dot d and inside this we have two parameter first parameter will be our string uh so i'm going to type here contact info comma and next i am going to type here name and plus data dot get i dot name again plus and secondly i have i will type here phone number concatenate this and again i will use my array name data dot get i dot phone number that's it press semicolon at the end so our code is completed now let me run the app so i can show you the output so to run your android app just select emulator from here select your device and click on this play icon so guys here you can see that our app has been run uh to our emulator this is here you can see that contact info name find gap and phone number this was our random phone number so let me click on app inspection and this is our database and here you can see that we just have insert one row in our table and you can see that row easily to our uh locate so guys by using this technique we can retrieve data from our database and when i was running my application it gives me an error it was returning a null cursor because because of some reason the data was not available to my table that's why i just uncomment this line db helper dot add contacts so it just add a contact in my database and then my this code works and it have retrieved data to look at so guys i hope you must enjoy this video and if you have any question related to this tutorial you can ask a snagman session and you can also contact us from android apps development or web web development so we have a software house we have a team that can develop software's websites e-commerce websites shopify websites for you and if you have any question you can ask us in the comment section and please share this video with your those friends who want to learn android apps development and finally thanks for watching
Info
Channel: FineGap
Views: 4,291
Rating: undefined out of 5
Keywords: how to fetch data from sqlite database in android, how to fetch data from sqlite database in android studio, how to get data from sqlite database in android studio, fetch data from sqlite database, fetch data, fetch data from sqlite, how to fetch data, how to fetch data from sqlite, how to fetch data from database, sqlite, how to get data from database, get data in sqlite, fetch data in sqlite, database, How to Select data from the database, select data, get data from database, data
Id: HWI4y7TYFuw
Channel Id: undefined
Length: 10min 52sec (652 seconds)
Published: Sat Jul 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.