Hello friend,
Today we are going to learn about CRUD(Create,remove,update,delete) System in android.
Simplest way to create above definition in android below code to perform insert,update,delete,select in android application with SQLlite.
activity_main.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:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/fname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="First Name" />
<EditText
android:id="@+id/lname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Last Name" />
<EditText
android:id="@+id/mark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject Marks" />
<EditText
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ID" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="25dp"
android:gravity="center">
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Insert" />
<Button
android:id="@+id/select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_toRightOf="@id/add"
android:text="Show" />
<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/add"
android:layout_margin="20dp"
android:text="Update" />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/select"
android:layout_margin="20dp"
android:layout_toRightOf="@id/update"
android:text="Delete" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Use id for update,delete and select into above example"
android:textSize="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Thank You" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
DBHelper myDb;
EditText fname, lname, mark, id;
Button insert, select, delete, update;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fname = (EditText) findViewById(R.id.fname);
lname = (EditText) findViewById(R.id.lname);
mark = (EditText) findViewById(R.id.mark);
id = (EditText) findViewById(R.id.id);
insert = (Button) findViewById(R.id.add);
select = (Button) findViewById(R.id.select);
update = (Button) findViewById(R.id.update);
delete = (Button) findViewById(R.id.delete);
insert.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
myDb = new DBHelper(MainActivity.this);
boolean isInserted = myDb.insertData(fname.getText().toString(),
lname.getText().toString(),
mark.getText().toString());
if (isInserted == true)
Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Data not Inserted", Toast.LENGTH_LONG).show();
}
}
);
select.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
myDb = new DBHelper(MainActivity.this);
Cursor res = myDb.getAllData();
if (res.getCount() == 0) {
showMessage("Error", "Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("Id :" + res.getString(0) + "\n");
buffer.append("First Name :" + res.getString(1) + "\n");
buffer.append("Last Name :" + res.getString(2) + "\n");
buffer.append("Marks :" + res.getString(3) + "\n\n");
}
showMessage("Data", buffer.toString());
}
}
);
delete.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
myDb = new DBHelper(MainActivity.this);
Integer deletedRows = myDb.deleteData(id.getText().toString());
if (deletedRows > 0)
Toast.makeText(MainActivity.this, "Data Deleted", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Data not Deleted", Toast.LENGTH_LONG).show();
}
}
);
update.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
myDb = new DBHelper(MainActivity.this);
boolean isUpdate = myDb.updateData(id.getText().toString(),
fname.getText().toString(),
lname.getText().toString(),
mark.getText().toString());
if (isUpdate == true)
Toast.makeText(MainActivity.this, "Data Update", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Data not Updated", 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();
}
}
DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Studb";
public static final String TABLE_NAME = "studtable";
public static final String COL_1 = "ID";
public static final String COL_2 = "FNAME";
public static final String COL_3 = "LNAME";
public static final String COL_4 = "MARKS";
public DBHelper(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,MARKS INTEGER)");
}
@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, String marks) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, name);
contentValues.put(COL_3, surname);
contentValues.put(COL_4, marks);
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, String marks) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, id);
contentValues.put(COL_2, name);
contentValues.put(COL_3, surname);
contentValues.put(COL_4, marks);
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});
}
}
OUTPUT:-
Today we are going to learn about CRUD(Create,remove,update,delete) System in android.
Simplest way to create above definition in android below code to perform insert,update,delete,select in android application with SQLlite.
activity_main.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:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/fname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="First Name" />
<EditText
android:id="@+id/lname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Last Name" />
<EditText
android:id="@+id/mark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject Marks" />
<EditText
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ID" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="25dp"
android:gravity="center">
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Insert" />
<Button
android:id="@+id/select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_toRightOf="@id/add"
android:text="Show" />
<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/add"
android:layout_margin="20dp"
android:text="Update" />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/select"
android:layout_margin="20dp"
android:layout_toRightOf="@id/update"
android:text="Delete" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Use id for update,delete and select into above example"
android:textSize="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Thank You" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
DBHelper myDb;
EditText fname, lname, mark, id;
Button insert, select, delete, update;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fname = (EditText) findViewById(R.id.fname);
lname = (EditText) findViewById(R.id.lname);
mark = (EditText) findViewById(R.id.mark);
id = (EditText) findViewById(R.id.id);
insert = (Button) findViewById(R.id.add);
select = (Button) findViewById(R.id.select);
update = (Button) findViewById(R.id.update);
delete = (Button) findViewById(R.id.delete);
insert.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
myDb = new DBHelper(MainActivity.this);
boolean isInserted = myDb.insertData(fname.getText().toString(),
lname.getText().toString(),
mark.getText().toString());
if (isInserted == true)
Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Data not Inserted", Toast.LENGTH_LONG).show();
}
}
);
select.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
myDb = new DBHelper(MainActivity.this);
Cursor res = myDb.getAllData();
if (res.getCount() == 0) {
showMessage("Error", "Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("Id :" + res.getString(0) + "\n");
buffer.append("First Name :" + res.getString(1) + "\n");
buffer.append("Last Name :" + res.getString(2) + "\n");
buffer.append("Marks :" + res.getString(3) + "\n\n");
}
showMessage("Data", buffer.toString());
}
}
);
delete.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
myDb = new DBHelper(MainActivity.this);
Integer deletedRows = myDb.deleteData(id.getText().toString());
if (deletedRows > 0)
Toast.makeText(MainActivity.this, "Data Deleted", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Data not Deleted", Toast.LENGTH_LONG).show();
}
}
);
update.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
myDb = new DBHelper(MainActivity.this);
boolean isUpdate = myDb.updateData(id.getText().toString(),
fname.getText().toString(),
lname.getText().toString(),
mark.getText().toString());
if (isUpdate == true)
Toast.makeText(MainActivity.this, "Data Update", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Data not Updated", 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();
}
}
DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Studb";
public static final String TABLE_NAME = "studtable";
public static final String COL_1 = "ID";
public static final String COL_2 = "FNAME";
public static final String COL_3 = "LNAME";
public static final String COL_4 = "MARKS";
public DBHelper(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,MARKS INTEGER)");
}
@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, String marks) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, name);
contentValues.put(COL_3, surname);
contentValues.put(COL_4, marks);
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, String marks) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, id);
contentValues.put(COL_2, name);
contentValues.put(COL_3, surname);
contentValues.put(COL_4, marks);
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});
}
}
OUTPUT:-
awesome buddy,
ReplyDeleteThanks it's save me.