Working on FIW.ME!

We are currently working on a next generation URL shortener!

Android And Database; Creating Helper Class and Other Necessary Setups to Begin With Database Programming In Android.

Android is provided with many options to save and read custom user produced data, among them are Saved Preferences, Save data to Internal or External Storage, and Save data to Database.

Saving data to database is considered to be very easy, in certain cases, such as saving structured data as in table of data.

Android database is simple to use, it uses SQLite system and the data stored in database is stored separate from other apps data, ensuring the security/privatism.

Sure enough, you are here to know less about database but to know more about how to implement it in android applications. But, before we begin, it is be understood that this is a long session of lessons and the lessons will be updated periodically.

What we are going to learn here today?

  • Create Table structure Class
  • Creating Open Helper Class
  • A function to insert data to table
  • Using the above to implement simple Address Book

Okay, before we start here is little more briefing, what we are doing here is creating a simple app that has five fields, First name, Last Name, Address, Email and Phone No, we take that data from the user entering and we store the same data to the database for later access.

What we in this article are really focusing on is the steps to create database, table and storing the data to the table, that is all. Nothing complicated in this articles.

Create Table structure class, What is table structure class? it is nothing mandatory, it is purely for ease of use, it holds string values for column names, that is all.  We can skip creating this class, but then you will have to remember all the table structure by-heart.

Above code is structure code for table structure, it holds 7 field names, first one is TABLE_NAME this holds the name for the table. Next one is ‘ _id ‘ it is not user modifiable because it going to be primary key and also auto incremented. It is helpful in selecting data from the table.

Next are the first name, last name, address, email and phone, these all are user filled and will be pulled from EditText fields of the application.

Creating Open Helper Class

Open Helper class is one we call every time to create and establish database connection, and from within which we are going to perform the database queries and etc. 

Here is the code for the SQLiteOpenHelper class:

above class is named userDBHelper extending SQLiteOpenHelper. Read the comments in the code to understand the code, carefully.

In the above class we had to override onCreate and onUpgrade Methods. onCreate method is called first time the database is created, hence there is where we are putting create table query.

The above code is used to make query to the database. In above code it might seem a confusing structure to handle. But, in reality, if we replaced the variables with their actual fields, we are just passing the SQL query shown below:

it is simple query code.

  • A function to insert data to table

Now, skipping to the addUser function, this is the function we are going to use to add the new users to the table.

There are multiple methods to insert values into the database, like sending raw query with data values embedded in them, or passing query with arguments, and this method pass key values, this is easier one. Hence we move on with this.

We create Content Value class to store values with the names corresponding to table name. ContentValues.put(“coloumnName”,”value”);  format.

and then we establish a writable instance with the database using

and then we pass the Content Values to  ‘insert‘ method, with three fields, first is table name, second one null coloumn hack, and the third is ContentValues.

The same returns long int which is number of affected rows, it may return -1, indicating failure in execution.

Now that we have created background classes for performing database operation, we shall now see how to link you current application to these classes.

Using the above to implement simple address book

The simple address book here is not a very functional application yet, which we will develop further in future with more coding tutorials.

But, for now this app is useful enough to take user data using EditText fields and store them into database, systematically.

This is how final app will look like. 

Start an Empty Project in android studio, name it anything you like.

To the current Layout file, add this code.

Above code adds:

  • A linear layout, to hold the field in vertical fashion.
  • Five EditText fields, one for each First Name, Last Name, Email , Address and Phone Number.
  • One Button, to confirm save data.

Finally add, the functional code to the MainActivity.