Android – Creating Registration Page using SQLITE Database

In this tutorial we will learn to create a registration page using SQLITE database in Android. Here you will get the concept of inserting data inside the SQLITE database. Also you will get to know about fetching data from SQLITE database. So let’s start.

Creating a New Android Project

  • As we all know the steps to make a new Android Project, so we not discussing it here. If you want to learn about that you can check this tutorial Android – Creating First New Project – 1 .
  • Take it as blank activity and move forward.
  • Now we will get our project structure.

Creating our Layout Design

  • Go to res –> layout –> activity_main.xml and copy the below code and save the file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/LinearLayout1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context=".MainActivity" >

<EditText
 android:id="@+id/editText1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:ems="10"
 android:hint="Name of the Student" >

<requestFocus />
 </EditText>

<EditText
 android:id="@+id/editText2"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:ems="10"
 android:hint="Address" />

<Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:onClick="click"
 android:text="Save" />

<Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:onClick="view"
 android:text="View" />

</LinearLayout>
  • So now our layout will look like this as shown in figure.

Creating a Java Class for Database Operation

  • Right Click on Project and go to File –> New –> Class 
  • Give the name of the class. Here we are giving it as Sqlite and click on Finish.
  • Now we will get the new java class in which we will define all the database operation i.e. we will create the database, table and insert our data.
  • Copy the below code and paste it in Sqlite.java file just below the package line and save the file.
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Sqlite {

 //Declaring variable for database name,table name and database version

String database = "db";
 String table = "user";
 int version = 1;

 //declaring variable for table column name

 String name = "name";
 String add = "address";
 String id = "id";

helper h;
 Context c;
 SQLiteDatabase s;

public class helper extends SQLiteOpenHelper {

public helper(Context context) {
 super(context, database, null, version);
 // TODO Auto-generated constructor stub
 }

@Override
 public void onCreate(SQLiteDatabase arg0) {

 // executing query for creating table

String query = "CREATE TABLE " + table + " ( " + id
 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + name 
 + " TEXT NOT NULL, " + add + " TEXT NOT NULL );";
 arg0.execSQL(query);

// TODO Auto-generated method stub

}

@Override
 public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

}

}

public Sqlite(MainActivity mainActivity) {
 c = mainActivity;

 }

public void open() {

h = new helper(c);
 s = h.getWritableDatabase();

}

public void save(String na, String ad) {

 ContentValues cv = new ContentValues(); 
 cv.put(name, na);
 cv.put(add, ad);
 s.insert(table, null, cv); //inserting data into database

}

public void close() {

s.close();

 }

public String get() {
 h = new helper(c);
 s = h.getReadableDatabase();
 String txt = "";
 String[] col = { id, name, add };
 Cursor c = s.query(table, col, null, null, null, null, null); //fetching data from database
 c.moveToFirst();
 for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

txt = txt + c.getString(0) + " " + c.getString(1) + " "
 + c.getString(2) + "\n";

}

return txt;
 }

}
  • Now open MainActivity.java and copy the below code there just below the package file as given below and save the file.
 
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

 EditText name,add;
 Sqlite sql;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 name=(EditText)findViewById(R.id.editText1);
 add=(EditText)findViewById(R.id.editText2);
 sql=new Sqlite(MainActivity.this);
 }

 public void click(View v)
 {
 String na=name.getText().toString();
 String ad=add.getText().toString();
 sql.open();
 sql.save(na,ad);
 sql.close();
 Toast.makeText(MainActivity.this, "Saved Successfully", Toast.LENGTH_SHORT).show();
 }

 public void view(View v)
 {

 String txt=sql.get();
 Dialog d=new Dialog(MainActivity.this);
 d.setTitle("My Data");
 TextView tv=new TextView(MainActivity.this);
 tv.setText(txt);
 d.setContentView(tv);
 d.show();

 }

}
  • Now we have to run the project. Right click on project, go to Run As –> Android Application.

Complete Video Tutorial :

Thank you for visiting our tutorial. If you have any doubts or queries regarding this, you can comment below in our comment section. Don’t forget to share the tutorial if you like it.

© Copyright 2016 – Apptronix Technologies. All Rights Reserved. ISO 9001:2015 Certified