SQLite CRUD in Android Studio: Insert, Update, Delete and View
Build a small Android app that saves people's details in a local SQLite database, then updates, deletes and lists them. It is the classic first database app: one screen, one helper class, and the four operations every data app needs.
What you'll learn
- Create a SQLite database and table with SQLiteOpenHelper
- Insert, update and delete rows with ContentValues
- Read rows with a Cursor and show them in a dialog
- Connect buttons in XML to Java code
Before you start
- Android Studio (any recent version)
- Basic Java: variables, methods and if/else
- An emulator or an Android phone
How it fits together
- 1activity_main.xml form
- 2MainActivity buttons
- 3DBHelper methods
- 4Userdetails table
Build it step by step
1. Create the project
In Android Studio choose New Project → Empty Views Activity. Set Language to Java and Minimum SDK to API 24. If you want to copy the code below without changes, use the package name com.example.sqlitecrud.
2. Build the form
Open activity_main.xml and add three EditText boxes (name, contact, date of birth) and four buttons: Insert, Update, Delete and View. Each view gets an id so Java can find it. The labels live in strings.xml, so the layout stays clean and the app is easy to translate.
3. Create the database helper
Add a new Java class called DBHelper that extends SQLiteOpenHelper.
- The constructor names the database file:
Userdata.db. onCreateruns once, the first time the app opens the database, and creates theUserdetailstable.nameis the primary key, so two people can't have the same name.
4. Insert a row
insertuserdata puts the three values into a ContentValues object and calls insert. It returns -1 if the row could not be saved, for example when the name already exists.
5. Update and delete
update and delete find the row by name with "name=?". Both return how many rows changed, so 0 means nobody with that name was found.
6. Read everything
getdata runs Select * from Userdetails and returns a Cursor. MainActivity walks through it with moveToNext(), builds one text block, and shows it in an AlertDialog.
7. Connect the buttons
In MainActivity, find each view with findViewById, create one DBHelper, and give every button a click listener that calls the matching method and shows a Toast.
8. Run and test
- Insert two people.
- Tap View to see them.
- Change one person's contact and tap Update.
- Delete one and tap View again.
Try inserting the same name twice: you'll see New Entry Not Inserted, because of the primary key.
If something goes wrong
- "Entry Not Updated" or "Entry Not Deleted": the name must match exactly, including capital letters.
- Where is my data? Open App Inspection → Database Inspector in Android Studio while the app runs, and you can see the
Userdetailstable live. - The form sits under the status bar: keep the
EdgeToEdgeand window-insets lines at the top ofonCreate.
What's improved over the video
The code matches the video, with three small upgrades: cursors are closed after use, onUpgrade recreates the table, and the screen respects Android 15's edge-to-edge layout.
Files affected
Check what to add and what to replace before copying code.
Application code
Select a file to see its code and where it belongs.
This code uses package com.example.sqlitecrud. Change that first line to your own package, the one at the top of your MainActivity.
app/src/main/java/com/example/sqlitecrud/MainActivity.javaConnects each button to a DBHelper method and shows the result.
package com.example.sqlitecrud;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
EditText name, contact, dob;
Button insert, update, delete, view;
DBHelper DB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
// Keep the form clear of the status bar and navigation bar (Android 15+ draws edge to edge).
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets bars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(bars.left, bars.top, bars.right, bars.bottom);
return insets;
});
name = findViewById(R.id.name);
contact = findViewById(R.id.contact);
dob = findViewById(R.id.dob);
insert = findViewById(R.id.btnInsert);
update = findViewById(R.id.btnUpdate);
delete = findViewById(R.id.btnDelete);
view = findViewById(R.id.btnView);
DB = new DBHelper(this);
insert.setOnClickListener(v -> {
String nameTXT = name.getText().toString();
String contactTXT = contact.getText().toString();
String dobTXT = dob.getText().toString();
Boolean checkinsertdata = DB.insertuserdata(nameTXT, contactTXT, dobTXT);
if (checkinsertdata)
Toast.makeText(MainActivity.this, "New Entry Inserted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "New Entry Not Inserted", Toast.LENGTH_SHORT).show();
});
update.setOnClickListener(v -> {
String nameTXT = name.getText().toString();
String contactTXT = contact.getText().toString();
String dobTXT = dob.getText().toString();
Boolean checkupdatedata = DB.updateuserdata(nameTXT, contactTXT, dobTXT);
if (checkupdatedata)
Toast.makeText(MainActivity.this, "Entry Updated", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "Entry Not Updated", Toast.LENGTH_SHORT).show();
});
delete.setOnClickListener(v -> {
String nameTXT = name.getText().toString();
Boolean checkudeletedata = DB.deletedata(nameTXT);
if (checkudeletedata)
Toast.makeText(MainActivity.this, "Entry Deleted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "Entry Not Deleted", Toast.LENGTH_SHORT).show();
});
view.setOnClickListener(v -> {
StringBuilder buffer = new StringBuilder();
// try-with-resources closes the cursor for us.
try (Cursor res = DB.getdata()) {
if (res.getCount() == 0) {
Toast.makeText(MainActivity.this, "No Entry Exists", Toast.LENGTH_SHORT).show();
return;
}
while (res.moveToNext()) {
buffer.append("Name: ").append(res.getString(0)).append("\n");
buffer.append("Contact: ").append(res.getString(1)).append("\n");
buffer.append("Date of Birth: ").append(res.getString(2)).append("\n\n");
}
}
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(true);
builder.setTitle("User Entries");
builder.setMessage(buffer.toString());
builder.show();
});
}
}


