1) DatabaseHelper.java
package com.example.admin.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Student.db";
public static final String TABLE_NAME = "student_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "SURNAME";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String name,String surname) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
public boolean updateData(String id,String name,String surname) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1,id);
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id });
return true;
}
public Integer deleteData (String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID = ?",new String[] {id});
}
}
2) MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/add_data"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:padding="15dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/et_name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Surname"
android:id="@+id/et_surname"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Data"
android:id="@+id/btn_add"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/edit_data"
android:layout_marginTop="20dp"
android:padding="15dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Id"
android:id="@+id/et_id_edit"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/et_name_edit"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Surname"
android:id="@+id/et_surname_edit"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit Data"
android:id="@+id/btn_edit"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/delete_data"
android:layout_marginTop="20dp"
android:padding="15dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Id"
android:id="@+id/et_id_delete"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Data"
android:id="@+id/btn_delete"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:padding="10dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Data"
android:id="@+id/btn_get"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
3) MainActivity.java
package com.example.admin.myapplication;
import android.app.AlertDialog;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btn_get, btn_add, btn_edit, btn_delete;
EditText et_name, et_surname, et_id_edit, et_name_edit, et_surname_edit, et_id_delete;
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DatabaseHelper(this);
btn_get = (Button)findViewById(R.id.btn_get);
btn_add = (Button)findViewById(R.id.btn_add);
btn_edit = (Button)findViewById(R.id.btn_edit);
btn_delete = (Button)findViewById(R.id.btn_delete);
//Add Data
et_name = (EditText)findViewById(R.id.et_name);
et_surname = (EditText)findViewById(R.id.et_surname);
// Update Data
et_id_edit = (EditText)findViewById(R.id.et_name_edit);
et_name_edit = (EditText)findViewById(R.id.et_name_edit);
et_surname_edit = (EditText)findViewById(R.id.et_surname_edit);
// Delete Data
et_id_delete = (EditText)findViewById(R.id.et_id_delete);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AddData();
}
});
btn_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UpdateData();
}
});
btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DeleteData();
}
});
}
public void viewAll() {
Cursor res = db.getAllData();
if(res.getCount() == 0) {
// show message
showMessage("Error","Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("Id :"+ res.getString(0)+"\n");
buffer.append("Name :"+ res.getString(1)+"\n");
buffer.append("Surname :"+ res.getString(2)+"\n");
buffer.append("Marks :"+ res.getString(3)+"\n\n");
}
// Show all data
showMessage("Data",buffer.toString());
}
public void DeleteData() {
Integer deletedRows = db.deleteData(et_id_delete.getText().toString());
if(deletedRows > 0) {
Toast.makeText(MainActivity.this,"Data Deleted",Toast.LENGTH_LONG).show();
et_id_delete.setText("");
} else{
Toast.makeText(MainActivity.this,"Data not Deleted",Toast.LENGTH_LONG).show();
}
}
public void UpdateData() {
boolean isUpdate = db.updateData(et_id_edit.getText().toString(),
et_name_edit.getText().toString(),
et_surname_edit.getText().toString());
if(isUpdate == true) {
Toast.makeText(MainActivity.this,"Data Update",Toast.LENGTH_LONG).show();
et_id_edit.setText("");
et_name_edit.setText("");
et_surname_edit.setText("");
} else{
Toast.makeText(MainActivity.this,"Data not Updated",Toast.LENGTH_LONG).show();
}
}
public void AddData() {
boolean isInserted = db.insertData(et_name.getText().toString(),
et_surname.getText().toString());
if(isInserted == true)
{
Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
et_name.setText("");
et_surname.setText("");
} else{
Toast.makeText(MainActivity.this,"Data not Inserted", Toast.LENGTH_LONG).show();
}
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
package com.example.admin.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Student.db";
public static final String TABLE_NAME = "student_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "SURNAME";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String name,String surname) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
public boolean updateData(String id,String name,String surname) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1,id);
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id });
return true;
}
public Integer deleteData (String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID = ?",new String[] {id});
}
}
2) MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/add_data"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:padding="15dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/et_name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Surname"
android:id="@+id/et_surname"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Data"
android:id="@+id/btn_add"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/edit_data"
android:layout_marginTop="20dp"
android:padding="15dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Id"
android:id="@+id/et_id_edit"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/et_name_edit"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Surname"
android:id="@+id/et_surname_edit"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit Data"
android:id="@+id/btn_edit"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/delete_data"
android:layout_marginTop="20dp"
android:padding="15dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Id"
android:id="@+id/et_id_delete"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Data"
android:id="@+id/btn_delete"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:padding="10dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Data"
android:id="@+id/btn_get"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
3) MainActivity.java
package com.example.admin.myapplication;
import android.app.AlertDialog;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btn_get, btn_add, btn_edit, btn_delete;
EditText et_name, et_surname, et_id_edit, et_name_edit, et_surname_edit, et_id_delete;
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DatabaseHelper(this);
btn_get = (Button)findViewById(R.id.btn_get);
btn_add = (Button)findViewById(R.id.btn_add);
btn_edit = (Button)findViewById(R.id.btn_edit);
btn_delete = (Button)findViewById(R.id.btn_delete);
//Add Data
et_name = (EditText)findViewById(R.id.et_name);
et_surname = (EditText)findViewById(R.id.et_surname);
// Update Data
et_id_edit = (EditText)findViewById(R.id.et_name_edit);
et_name_edit = (EditText)findViewById(R.id.et_name_edit);
et_surname_edit = (EditText)findViewById(R.id.et_surname_edit);
// Delete Data
et_id_delete = (EditText)findViewById(R.id.et_id_delete);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AddData();
}
});
btn_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UpdateData();
}
});
btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DeleteData();
}
});
}
public void viewAll() {
Cursor res = db.getAllData();
if(res.getCount() == 0) {
// show message
showMessage("Error","Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("Id :"+ res.getString(0)+"\n");
buffer.append("Name :"+ res.getString(1)+"\n");
buffer.append("Surname :"+ res.getString(2)+"\n");
buffer.append("Marks :"+ res.getString(3)+"\n\n");
}
// Show all data
showMessage("Data",buffer.toString());
}
public void DeleteData() {
Integer deletedRows = db.deleteData(et_id_delete.getText().toString());
if(deletedRows > 0) {
Toast.makeText(MainActivity.this,"Data Deleted",Toast.LENGTH_LONG).show();
et_id_delete.setText("");
} else{
Toast.makeText(MainActivity.this,"Data not Deleted",Toast.LENGTH_LONG).show();
}
}
public void UpdateData() {
boolean isUpdate = db.updateData(et_id_edit.getText().toString(),
et_name_edit.getText().toString(),
et_surname_edit.getText().toString());
if(isUpdate == true) {
Toast.makeText(MainActivity.this,"Data Update",Toast.LENGTH_LONG).show();
et_id_edit.setText("");
et_name_edit.setText("");
et_surname_edit.setText("");
} else{
Toast.makeText(MainActivity.this,"Data not Updated",Toast.LENGTH_LONG).show();
}
}
public void AddData() {
boolean isInserted = db.insertData(et_name.getText().toString(),
et_surname.getText().toString());
if(isInserted == true)
{
Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
et_name.setText("");
et_surname.setText("");
} else{
Toast.makeText(MainActivity.this,"Data not Inserted", Toast.LENGTH_LONG).show();
}
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}