How to check data in SQLite Database

Hello Everyone!

Today, I will show you how to check the SQLite Database.

Last week, I created a tutorial of how to do a registration page and storing data into SQLite Database.  In today’s tutorial, I will show you how to check that data.

First things first! Download DB Browser for SQLite and install it into your computer.  We will be using DB Browser for SQLite to view .db files.

db.PNG

Now go onto Android Studio and go to Tools > Android > Android Device Monitor to open up .  I couldn’t get it to open from Android Device Monitor, so I did a manual open instead.  What we want is the “monitor” application in the folder Users > AppData > Local > Android > sdk > tools > lib > monitor-x86, mine is an x86 machine, so I had to do this.

Regardless you should see the Android Device Monitor popup like below:

pop

Click on your virtual device and navigate down to data > data > [package name] > data > User.db that you created, and then on the top right, click “Pull a file from the device” and save it anywhere.

pull

Next open up DB Browser for SQLite, Click File > open database and select the User.db file.  Then click the “Browse Database” tab, and then select the User.db file.  It should have the following information:

database.PNG

Great!!! Now we know for sure that the data is being inserted into the database!  Now we can easily query the information and set these information to some TextViews.

Let’s continue from the last tutorial for simplicity.  We left off at defining “NextActivity” so now let’s use NextActivity to display the data we stored after registering.  Go to the activity_next.xml in the layout folder.  Let’s use TextViews to display the data stored from earlier, and a button to click on to display the data.  Give each their respective IDs, and define it in the java file.

next

And set each TextView and Button to their id’s in the java file.  when the button Display Data is clicked, we will call the function displayData()

Now that we have set up what the activity will do (display the data stored from the registration activity), let’s go ahead and define the displayData() function.  Go back to the DatabaseHelper.java file and create a new function called getAllData() which will get all the data on the database.  We will define what this query is with the following code:

asd

We define an SQLiteDatabase instance and set it to the built-in getWritableDatabase().  Then we define a Cursor instance (Cursor being the pointer to the read-write information of the database) and set it to what we want to query.  We want all the information on the database, so we’ll use SELECT * FROM user_table which means to select all from the user database table.

If we want a more direct and specific query, for example, querying a specific user such as myself and my phone number, then we can do SELECT FirstName, Phone FROM user_table WHERE FirstName=”Matthew”.  This will display only the matching first name “Matthew” and Matthew’s phone number.

Now all we have to do is call getAllData() in the displayData() function of NextActivity.

displayData.PNG

And that’s it!! Let’s test it on the Emulator.

info

AWESOME! The data is displayed as it should be, and we have successfully queried the data from SQLite Database!

data

That’s it for now! Stay tune as my next tutorial will be on how to update / add / delete information from the database.

Thanks! and Happy Coding!

Below is the code:

Continue reading “How to check data in SQLite Database”

Register Tutorial

Hello everyone!

Today, I will be covering how to create the register page of your app using Android Studio.

First off, start a new project by clicking Files > New > New Project.  Name the project RegisterTutorial or anything you choose.

register_new_project

Now choose the Phone and Tablet category, and set the minimum SDK you want for the lowest devices that can use your application. I’ve used the default API 15: Android 4.0.3 (Ice Cream Sandwich) which will run on 100% of devices.

2

Next, choose “Empty Activity” because we will be adding onto it.

3

Finally, name the new activity.

4

Wait until the project builds and synchronize, then navigate to the layout you created with the new Activity.  Make sure it is ConstraintLayout because in this way, we can align the EditText fields and Buttons according to the phone’s screen.5

Click on “Design,” delete the “Hello World” in the center and add in the desired number of EditText fields.  I’ve used a total of 6 EditTexts.  Click on the EditText and drag the left side to the left of the phone and the right side to the right of the phone, so that the EditText is centered.  Do the same for the remaining five.  Finally add in two buttons, one to register the information to store in the database, and the other to cancel the registration.

When you click on the edittexts, there should be a right popup of information for the edittext.  Make sure to delete the “text” field of the EditText, then type in the desired text you want for the user to input in the “hint” field.  Also, change the id name to distinguish and identify them later.

6

We’re almost done!! After aligning the EditTexts and Buttons, we can now move onto Java Coding! Are you ready?

Now move onto the “MainActivity.java” file. It should look like the below.

7.PNG

We are going to identify the EditText fields and Buttons that we created in the layout.  Declare the EditText field and Button variables and assign them accordingly as below.

8.PNG

Now, we have to set what happens when the register button is clicked, or when the cancel button is clicked.  Ideally, if register button is clicked, then we will register the inputted texts in the EditTexts, and if cancel is clicked, then we can just cancel the current activity.  We’ll define register() and cancel() as functions of when each button is clicked.

In the register function, we want to get the information from the EditText fields.  We also want to show a progress dialog and make it appear when the user clicks register so that the app is actually showing that the user is registering their information rather than them waiting.  We can do this like the following:

9

We get the text from the EditText fields into strings, and then we trim it so that we remove white spaces.

Next, we have to save the information into some database.  At this point, you have the option to choose your database.  For simplicity, let’s use the built-in SQLite Database.  To do so, we will have to create a new java class “DatabaseHelper”

Right click your project files (in my case it is com.tutorials.mouacoding.registertutorial) and select New > Java Class, and name it “DatabaseHelper”

10

Let’s fill in this class. Ready? Set? GO!

First we’ll define the database name, call it “User.db”, the table name “user_table” and each of the columns (ID, First name, last name, email, password, phone).

in the OnCreate method, we’ll use db.execSQL to create a table of the table name, and then the desired column order.  In the onUpgrade method, we’ll drop the table if the table already exists.

Finally, we’ll define the insertData() function, setting the column values and finally using db.insert() to store the values into the SQLite Database.  I’ve set long result = db.insert because the returned value is 1 if it was able to store, otherwise 0 if not.

getdata.PNG

We’re almost done!! Now, let’s call the insertData() in our register() function from the MainActivity. But first lets create a new activity so that once we finish registering, it will continue to the next activity of the app.

next

Okay, now onto business!! let’s insert into the database now. We’ll write the register() function as below.

First, we define the DatabaseHelper variable, and progress dialog.  Then, we’ll use an if statement to check if the password and confirm password equal, if they are then we can continue inserting.  call myDB.insert(string fields…) and then finally dismiss the progress dialog once the action is done.  We can check if it inserted or not by setting the boolean declared earlier to true or false via the db.insert call() and if it is inserted correctly, then go to the next activity.

We also define the cancel button by simply finish() which finishes the current activity and should thus go back to the “Login Activity.”

12

Now we test everything so far! Looks like it is working.

info

That’s it!! Check out the next tutorial as I show you how to check if the the database is inserted correctly!

Thanks and Happy Coding!

Continue reading “Register Tutorial”