Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

How to Delete a Value from SQLite Database in Android app

For Creating SQLite Database Click Here
For Inserting in SQLite Database Click Here
For Displaying from SQLite Database Click Here
For Updating in SQLite Database Click Here


In this, We Delete value from our SQLite Database.



For that, we need a Unique Id and on behalf of that Id, we will delete values from Database.

Then create a Delete Button in MainActivity.XML

MainActivity.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout 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"  
   android:orientation="vertical"  
   tools:context=".MainActivity">  
 <EditText  
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"  
   android:id="@+id/name"  
   android:hint="name"  
   />  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:id="@+id/marks"  
     android:hint="marks"  
     />  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:id="@+id/ids"  
     android:hint="id"  
     />  
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Insert"  
     android:id="@+id/Button"/>  
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Show"  
     android:id="@+id/show"/>  
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Update"  
     android:id="@+id/Update"/>  

   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Delete"  
     android:id="@+id/delete"/>  
 </LinearLayout>  

Here, We add a new Delete Button to Delete a value from Database.

Then, We create a new Delete Method in our DatabaseHelper.class

DatabaseHelper.class
 package harpreet.studio.mysqlitedatabase;  
 import android.app.ActionBar;  
 import android.content.ContentValues;  
 import android.content.Context;  
 import android.database.Cursor;  
 import android.database.sqlite.SQLiteDatabase;  
 import android.database.sqlite.SQLiteOpenHelper;  
 import android.provider.ContactsContract;  
 import androidx.annotation.Nullable;  
 public class DatabaseHelper extends SQLiteOpenHelper {  
   public static final String Database_name = "Students.db";  
   public static final String Table_name = "Student_table";  
   public static final String col_id = "Id";  
   public static final String col_name = "name";  
   public static final String col_marks = "marks";  
   public DatabaseHelper(@Nullable 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 , marks INTEGER)");  
   }  
   @Override  
   public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {  
       sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+Table_name);  
       onCreate(sqLiteDatabase);  
   }  
   public boolean insertData(String name,String marks)  
   {  
     SQLiteDatabase db = this.getWritableDatabase();  
     ContentValues cv = new ContentValues();  
     cv.put(col_name,name);  
     cv.put(col_marks,marks);  
     Long result = db.insert(Table_name,null,cv);  
     if(result == -1 )  
     {  
       return false;  
     }  
     else  
     {  
       return true;  
     }  
   }  
   public Cursor Showdata()  
   {  
     SQLiteDatabase db = this.getWritableDatabase();  
     Cursor cursor = db.rawQuery("select * from "+Table_name,null);  
     return cursor;  
   }  
   public boolean update(String id,String name,String marks)  
   {  
     SQLiteDatabase db = this.getWritableDatabase();  
     ContentValues cv = new ContentValues();  
     cv.put(col_id,id);  
     cv.put(col_name,name);  
     cv.put(col_marks,marks);  
     db.update(Table_name,cv,"Id = ?",new String[] { id });  
     return true;  
   }  

   public Integer delete(String id)  
   {  
     SQLiteDatabase db = this.getWritableDatabase();  
     return db.delete(Table_name,"Id = ?",new String[] {id});  
   }  
 }  

Here, We add a new Delete method to delete some values on behalf of Id.

Then we have to call that delete Button and create a Delete Method in our MainActivity.Java file

MainActivity.java
 package harpreet.studio.mysqlitedatabase;  
 import androidx.appcompat.app.AlertDialog;  
 import androidx.appcompat.app.AppCompatActivity;  
 import android.database.Cursor;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.Toast;  
 public class MainActivity extends AppCompatActivity {  
   DatabaseHelper mydb;  
   EditText etname,etmarks,etid;  
   Button insertbtn,showbtn,updatebtn,deletebtn;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     mydb = new DatabaseHelper(this);  
     etname = findViewById(R.id.name);  
     etmarks = findViewById(R.id.marks);  
     etid = findViewById(R.id.ids);  
     insertbtn = findViewById(R.id.Button);  
     showbtn = findViewById(R.id.show);  
     updatebtn = findViewById(R.id.Update);  
     deletebtn = findViewById(R.id.delete);  
     insertdata();  
     showdata();  
     update();  
     delete();  
   }  
   public void insertdata()  
   {  
     insertbtn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         Boolean Inserted = mydb.insertData(etname.getText().toString(),etmarks.getText().toString());  
         if(Inserted)  
         {  
           Toast.makeText(MainActivity.this, "Data is Inserted", Toast.LENGTH_SHORT).show();  
         }  
         else  
         {  
           Toast.makeText(MainActivity.this, "Error Inserting", Toast.LENGTH_SHORT).show();  
         }  
       }  
     });  
   }  
   public void showdata()  
   {  
     showbtn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         Cursor cursor = mydb.Showdata();  
         if(cursor.getCount() == 0)  
         {  
           message("Error","No data");  
           return;  
         }  
         StringBuffer buffer = new StringBuffer();  
         while(cursor.moveToNext())  
         {  
           buffer.append("Id : " + cursor.getString(0)+"\n")  
               .append("Name : "+ cursor.getString(1)+"\n")  
               .append("marks : "+ cursor.getString(2)+"\n");  
         }  
           message("Data",buffer.toString());  
       }  
     });  
   }  
   public void update()  
   {  
     updatebtn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         Boolean updated = mydb.update(etid.getText().toString(),etname.getText().toString(),  
             etmarks.getText().toString());  
         if(updated)  
         {  
           Toast.makeText(MainActivity.this, "Updated", Toast.LENGTH_SHORT).show();  
         }  
         else  
         {  
           Toast.makeText(MainActivity.this, "Error in Updating", Toast.LENGTH_SHORT).show();  
         }  
       }  
     });  
   }  
   public void delete()  
   {  
     deletebtn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         Integer delete = mydb.delete(etid.getText().toString());  
         if(delete > 0)  
         {  
           Toast.makeText(MainActivity.this, "Data Deleted", Toast.LENGTH_SHORT).show();  
         }  
         else  
         {  
           Toast.makeText(MainActivity.this, "Data Not Deleted", Toast.LENGTH_SHORT).show();  
         }  
       }  
     });  
   }  
   public void message(String title,String message)  
   {  
     AlertDialog.Builder builder = new AlertDialog.Builder(this);  
     builder.setCancelable(true);  
     builder.setTitle(title)  
         .setMessage(message)  
         .show();  
   }  
 }  

Here, We create a new Delete method and pass the value of Id in that method.

For Creating SQLite Database Click Here
For Inserting in SQLite Database Click Here
For Displaying from SQLite Database Click Here
For Updating in SQLite Database Click Here
For Deleting from SQLite Database Click Here

Follow us for more posts like this,
Subscribe Harpreet studio on Youtube
Like Harpreet Studio on Facebook

   

1 comment:

  1. Today, sports activities betting is a serious business and attracts plenty of customers. For example, in football betting, the most well-liked are match odds, under/over a certain number of goals and the correct score. Mr Green provides odds for every of those prospects, so you simply choose the market you wish to wager on. Multiple bets, identified as|also called|also 우리카지노 referred to as} accumulators, permit you to combine several of} different bets in a single wager and obtain high odds. An example of an accumulator can be combining three different teams to win their matches or five video games by which you assume there might be over 2.5 goals. Please observe that a maximum of 12 outcomes can be added to the betslip.

    ReplyDelete