In This tutorilas we will implement SQLIte Database in Android.The SQLite is a opensourec database and that used SQL Database. The SQLIte Store data in Text File on Device.SQlite support all Relational database feature in android.
The SQLite is light weight database, which comes to Android OS. So ,we will implement the SQlite
Database.
First you have create table with help of DatabaseOpenHelper class in our project.
...................................................................................................................................................................
package com.androidbeginner.testcode;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseOpenHelper extends SQLiteOpenHelper {
public static final String DBName = "TiffinCutomer.db";
public static final String TABLE_CUSTOMER = "CutomerRegistration_Table";
// create table for CUTOMER_INFO
public static final String TABLE_CUTOMER_INFO = "Create Table "
+ TABLE_CUSTOMER + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "CustomerId TEXT, "
+ "Name TEXT, "
+ "Mobile TEXT, "
+ "Email TEXT, "
+ "Password TEXT);";
//+ "State TEXT);";
public DatabaseOpenHelper(Context context) {
super(context, DBName, null, 1);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(TABLE_CUTOMER_INFO);
}
public void onUpgrade(SQLiteDatabase database, int _oldVersion,
int _newVersion) {
Log.w("TaskDBAdapter", "Upgrading from version " + _oldVersion + " to "
+ _newVersion + ", which will destroy all old data");
database.execSQL("DROP TABLE IF EXISTS " + TABLE_CUSTOMER);
onCreate(database);
}
}
Then you have create DatabaseAdpter Class in our project, and create some method a.e:-
INSERT,
DELETE,
UPDATE,
OTHER CONDITIONS etc
.................................................................................................................................................
package com.androidbeginner.testcode;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class DatabaseAdapter {
public static final String TABLE_CUSTOMER = "CutomerRegistration_Table";
public SQLiteDatabase database;
public DatabaseOpenHelper dbHelper;
public DatabaseAdapter(Context context) {
dbHelper = new DatabaseOpenHelper(context);
}
//Open Database
public void open() {
database = dbHelper.getWritableDatabase();
}
// close database
public void close() {
database.close();
}
// Insert the Users info in table
public long CUSTOMER_EntityInsert(String CustomerId, String Name,
String Mobile, String Email, String Password) {
ContentValues values = new ContentValues();
values.put("CustomerId", CustomerId);
values.put("Name", Name);
values.put("Mobile", Mobile);
values.put("Email", Email);
values.put("Password", Password);
return database.insert(TABLE_CUSTOMER, null, values);
}
// get the all database Index value
public ArrayList<String> getCustomerEntityDetails() {
ArrayList<String> list = new ArrayList<String>();
database = this.getReadableDatabase();
String selectQuery = "SELECT * FROM CutomerRegistration_Table";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(cursor.getColumnIndex("CustomerId")));
list.add(cursor.getString(cursor.getColumnIndex("Name")));
list.add(cursor.getString(cursor.getColumnIndex("Mobile")));
list.add(cursor.getString(cursor.getColumnIndex("Email")));
} while (cursor.moveToNext());
}
cursor.close();
return list;
}
// in this check the condition data Exist Or Not In table
public Boolean Checkconditon() {
boolean flag = false;
Cursor c = database.rawQuery(
"SELECT * FROM CutomerRegistration_Table", null);
if (c != null && c.getCount() >= 1) {
flag = true;
}
return flag;
}
// get the user password(used in login form)
public String GetPassword(String Email ) {
String password="";
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM CutomerRegistration_Table WHERE Email='"
+ Email + "' LIMIT 1";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
password= cursor.getString(cursor.getColumnIndex("Password"));
} while (cursor.moveToNext());
}
cursor.close();
return password;
}
// Read Database
@SuppressWarnings("unused")
private SQLiteDatabase getReadableDatabase() {
return database = dbHelper.getReadableDatabase();
}
//delete CutomerRegistration_Table
public void deleteCUSTOMER_Entity() {
database.delete(TABLE_CUSTOMER, null, null);
}
}
login_actvity.xml
.............................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dip"
android:orientation="vertical"
>
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:ems="10"
android:hint="Enter Email Id"
android:inputType="textEmailAddress"
android:textColorHint="@color/hint_edittextcolor"
android:textSize="14sp" />
<requestFocus />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"
android:layout_marginTop="8dip"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword"
android:textColorHint="@color/hint_edittextcolor"
android:textSize="14sp" />
<Button
android:id="@+id/btn_signUp"
android:layout_width="fill_parent"
android:layout_height="47dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="40dp"
android:background="@drawable/button_action"
android:text="Login"
android:textColor="@color/white" />
</LinearLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="2"
android:layout_margin="5dip" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip" >
<TextView
android:id="@+id/textView_registration"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_gravity="right"
android:layout_marginRight="12dp"
android:gravity="right|center"
android:text="Registration"
android:textColor="@color/yellow"
android:textSize="20sp"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:textStyle="bold|italic" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
registration_activity.xml
..........................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:ignore="ScrollViewSize" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/edit_text_gradient"
android:orientation="vertical"
android:padding="5dp" >
<EditText
android:id="@+id/editText_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:ems="10"
android:hint="Enter Name"
android:inputType="textPersonName"
android:textSize="14sp" >
</EditText>
<EditText
android:id="@+id/editText_mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:ems="10"
android:hint="Enter Mobile Number"
android:inputType="phone"
android:textColorHint="@color/hint_edittextcolor"
android:textSize="14sp" />
<EditText
android:id="@+id/editText_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:ems="10"
android:hint="Enter Email Id"
android:inputType="textEmailAddress"
android:textColorHint="@color/hint_edittextcolor"
android:textSize="14sp" />
<EditText
android:id="@+id/editText_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword"
android:textColorHint="@color/hint_edittextcolor"
android:textSize="14sp" />
<EditText
android:id="@+id/editText_cofPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:ems="10"
android:hint="Enter Confirm Password"
android:inputType="textPassword"
android:textColorHint="@color/hint_edittextcolor"
android:textSize="14sp" />
</LinearLayout>
<Button
android:id="@+id/btn_signUp"
android:layout_width="fill_parent"
android:layout_height="47dp"
android:layout_marginBottom="7dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="30dp"
android:text="Registration" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Registration.java
................................................................................................................................................................
package com.androidbeginner.testcode;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Registrations extends Activity implements OnFocusChangeListener,
android.view.View.OnClickListener {
EditText edit_name, edit_mobile, edit_email,editText_cofPassword,editText_password;
String string_name, string_mobile, string_email,string_password ;
String customerId="";
Button btn_signUp;
DatabaseAdapter SQliteDataBase = new DatabaseAdapter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
SQliteDataBase.open();
SharedPreferences sharedpreferences = this.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
if (SQliteDataBase.Checkconditon() == true) {
Toast.makeText(getApplication(), "exist", Toast.LENGTH_LONG).show();
}
setContentView(R.layout.registration_form);
edit_name = (EditText) findViewById(R.id.editText_name);
edit_mobile = (EditText) findViewById(R.id.editText_mobile);
edit_email = (EditText) findViewById(R.id.editText_email);
editText_password = (EditText) findViewById(R.id.editText_password);
editText_cofPassword = (EditText) findViewById(R.id.editText_cofPassword);
btn_signUp = (Button) findViewById(R.id.btn_signUp);
btn_signUp.setOnClickListener(this);
edit_name.setOnFocusChangeListener(this);
edit_mobile.setOnFocusChangeListener(this);
edit_email.setOnFocusChangeListener(this);
editText_password.setOnFocusChangeListener(this);
editText_cofPassword.setOnFocusChangeListener(this);
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
switch (v.getId()) {
case R.id.editText_name:
edit_name.setError(null);
break;
case R.id.editText_mobile:
edit_mobile.setError(null);
break;
case R.id.editText_email:
edit_email.setError(null);
case R.id.editText_password:
edit_mobile.setError(null);
break;
case R.id.editText_cofPassword:
edit_email.setError(null);
default:
break;
}
}
@Override
public void onClick(View v) {
if (edit_name.getText().toString().equals("")) {
edit_name.setError("Enter Name");
edit_name.requestFocus();
} else if (editText_password.getText().toString().equals("")) {
editText_password.setError("Enter Email Address");
editText_password.requestFocus();
} else if (editText_cofPassword.getText().toString().equals("")) {
editText_cofPassword.setError("Enter Email Address");
editText_cofPassword.requestFocus();
}else if (!editText_cofPassword.getText().toString().equals(editText_password.getText().toString())) {
Toast.makeText(getApplication(), "Don't Match Password", Toast.LENGTH_LONG).show();
} else if (edit_email.getText().toString().equals("")) {
edit_email.setError("Enter Email Address");
edit_email.requestFocus();
}
else if (!edit_email.getText().toString()
.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")) {
edit_email.setError("Enter valid Email address");
edit_email.requestFocus();
} else if (edit_mobile.getText().toString().equals("")) {
edit_mobile.setError("Enter Mobile Number");
edit_mobile.requestFocus();
} else if (edit_mobile.getText().toString().length() < 10
|| edit_mobile.getText().toString().length() > 10) {
edit_mobile.setError("Enter 10 Digt Number");
edit_mobile.requestFocus();
}
else {
string_name = edit_name.getText().toString();
string_mobile = edit_mobile.getText().toString();
string_email = edit_email.getText().toString();
string_password = editText_password.getText().toString();
SQliteDataBase.deleteCUSTOMER_Entity();
SQliteDataBase.open();
SQliteDataBase.CUSTOMER_EntityInsert("1008987",string_name, string_mobile, string_email,string_password );
Toast.makeText(getApplication(), "Registration successfull", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, Login_Activity.class);
startActivity(intent);
}
}
}
Login_Activity.java
..................................................................................................................................................
package com.androidbeginner.testcode;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Login_Activity extends Activity implements
android.view.View.OnClickListener {
TextView sginUp_btn;
EditText edit_email, editText_password;
String string_email, string_password;
Button btn_signIn;
DatabaseAdapter SQliteDataBase = new DatabaseAdapter(this);
ArrayList<HashMap<String, String>> ParameterList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
public static final String MyPREFERENCES = "MyPrefs";
public static final String Pssword = "PsswordKey";
public static final String Email = "emailKey";
SharedPreferences sharedpreferences;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle(" LogIn");
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
setContentView(R.layout.login_actvity);
edit_email = (EditText) findViewById(R.id.email);
editText_password = (EditText) findViewById(R.id.password);
sginUp_btn = (TextView) findViewById(R.id.textView_registration);
btn_signIn = (Button) findViewById(R.id.btn_signUp);
sginUp_btn.setOnClickListener(this);
btn_signIn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_signUp:
string_email=edit_email.getText().toString();
string_password=editText_password.getText().toString();
String user=SQliteDataBase.GetPassword(string_email);
if(string_password.equals(user)){
Toast.makeText(getApplicationContext(), "Login Sucessfull", Toast.LENGTH_LONG).show();
}
break;
case R.id.textView_registration:
Intent intent = new Intent(Login_Activity.this, Registrations.class);
startActivity(intent);
break;
default:
break;
}
}
}
0 comments