SQLite total_changes() फंक्शन
उदाहरण से पहले, Android में SQLite डेटाबेस को समझें। SQLite एक ओपन-सोर्स SQL डेटाबेस है जो डिवाइस पर टेक्स्ट फ़ाइल में डेटा स्टोर करता है।
total_changes() फंक्शन डेटाबेस कनेक्शन खुलने के बाद से हुए कुल पंक्ति परिवर्तनों (इन्सर्ट, अपडेट, डिलीट) की संख्या लौटाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText android:id="@+id/name" android:layout_width="match_parent"
android:hint="नाम दर्ज करें" android:layout_height="wrap_content" />
<EditText android:id="@+id/salary" android:layout_width="match_parent"
android:inputType="numberDecimal" android:hint="वेतन दर्ज करें"
android:layout_height="wrap_content" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:id="@+id/save" android:text="सेव करें"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:id="@+id/refresh" android:text="रिफ्रेश"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:id="@+id/update" android:text="अपडेट"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:id="@+id/delete" android:text="सभी हटाएं"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<ListView android:id="@+id/listView" android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
चरण 3: MainActivity.java
package com.example.myapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
EditText name, salary;
ArrayAdapter<String> arrayAdapter;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DatabaseHelper helper = new DatabaseHelper(this);
final ArrayList<String> arrayList = helper.getAllContacts();
name = findViewById(R.id.name);
salary = findViewById(R.id.salary);
listView = findViewById(R.id.listView);
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
findViewById(R.id.delete).setOnClickListener(v -> {
if (helper.deleteAll()) Toast.makeText(this, "हटाया गया", Toast.LENGTH_LONG).show();
else Toast.makeText(this, "नहीं हटाया गया", Toast.LENGTH_LONG).show();
});
findViewById(R.id.update).setOnClickListener(v -> {
String n = name.getText().toString().trim();
String s = salary.getText().toString().trim();
if (!n.isEmpty() && !s.isEmpty()) {
if (helper.update(n, s)) Toast.makeText(this, "अपडेट किया गया", Toast.LENGTH_LONG).show();
else Toast.makeText(this, "अपडेट विफल", Toast.LENGTH_LONG).show();
} else {
name.setError("नाम दर्ज करें");
salary.setError("वेतन दर्ज करें");
}
});
findViewById(R.id.refresh).setOnClickListener(v -> {
arrayList.clear();
arrayList.addAll(helper.getAllContacts());
arrayAdapter.notifyDataSetChanged();
});
findViewById(R.id.save).setOnClickListener(v -> {
String n = name.getText().toString().trim();
String s = salary.getText().toString().trim();
if (!n.isEmpty() && !s.isEmpty()) {
if (helper.insert(n, s)) Toast.makeText(this, "इन्सर्ट किया गया", Toast.LENGTH_LONG).show();
else Toast.makeText(this, "इन्सर्ट विफल", Toast.LENGTH_LONG).show();
} else {
name.setError("नाम दर्ज करें");
salary.setError("वेतन दर्ज करें");
}
});
}
}
चरण 4: DatabaseHelper.java - total_changes() का उपयोग
package com.example.myapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "SalaryDatabase";
public static final String TABLE_NAME = "SalaryDetails";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, name TEXT, salary REAL, datetime DEFAULT CURRENT_TIMESTAMP)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insert(String name, String salary) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("salary", salary);
return db.insert(TABLE_NAME, null, values) != -1;
}
// total_changes() - कुल परिवर्तनों की संख्या लौटाता है
public ArrayList<String> getAllContacts() {
SQLiteDatabase db = getReadableDatabase();
ArrayList<String> list = new ArrayList<>();
Cursor cursor = db.rawQuery("SELECT total_changes() AS fullname FROM " + TABLE_NAME, null);
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(cursor.getColumnIndexOrThrow("fullname")));
} while (cursor.moveToNext());
}
cursor.close();
return list;
}
public boolean update(String name, String salary) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("salary", salary);
return db.update(TABLE_NAME, values, "name = ?", new String[]{name}) > 0;
}
public boolean deleteAll() {
SQLiteDatabase db = getWritableDatabase();
return db.delete(TABLE_NAME, null, null) > 0;
}
}
ऐप चलाएं
डिवाइस कनेक्ट करें और ऐप चलाएं।

परिणाम में, यह रिकॉर्ड्स में कुल परिवर्तनों को 3 के रूप में दिखा रहा है। total_changes() सेशन-स्कोप्ड होता है और सभी टेबल्स के परिवर्तनों को गिनता है।