How to update data in 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
We update data on behalf of the unique id.
So now we also add an EditText of our Id variable and an Update Button in our MainActivity.XML,
MainActivity.XML
Here, we add an EditText for our Id variable so, on behalf of a unique Id variable, we update our data.
Also we add an update button to update that value.
Then we add a new Update method in our DatabaseHelper class
DatabaseHelper.java
Here, we add a new update method to update values in our SQLite Database on behalf of our Id.
Then we have to add EditText and our Button in Java class to perform Action.
MainActivity.java
Here we create a new Update method and pass all three values to update values in our SQLite DataBase.
For Creating SQLite Database Click Here
For Inserting in SQLite Database Click Here
For Displaying from 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
For Inserting in SQLite Database Click Here
For Displaying from SQLite Database Click Here
In this, we update data that is stored in the SQLite Database.
So now we also add an EditText of our Id variable and an Update Button in our 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"/>
</LinearLayout>
Here, we add an EditText for our Id variable so, on behalf of a unique Id variable, we update our data.
Also we add an update button to update that value.
Then we add a new Update method in our DatabaseHelper class
DatabaseHelper.java
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;
}
}
Here, we add a new update method to update values in our SQLite Database on behalf of our Id.
Then we have to add EditText and our Button in Java class to perform Action.
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;
@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);
insertdata();
showdata();
update();
}
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 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 Update method and pass all three values to update values in our SQLite DataBase.
For Creating SQLite Database Click Here
For Inserting in SQLite Database Click Here
For Displaying from 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
No comments