एंड्रॉइड SQLite में IN ऑपरेटर का उपयोग
यह ट्यूटोरियल IN ऑपरेटर का उपयोग करके मानों की सूची के विरुद्ध डेटा फ़िल्टर करने का तरीका दिखाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट फ़ाइल (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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="नाम दर्ज करें"
android:layout_marginBottom="12dp"/>
<EditText
android:id="@+id/etSalary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="वेतन दर्ज करें"
android:inputType="numberDecimal"
android:layout_marginBottom="16dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="16dp">
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="सेव करें"
android:layout_marginEnd="8dp"/>
<Button
android:id="@+id/btnRefresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="रिफ्रेश"/>
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
चरण 3: DatabaseHelper.java
package com.example.sqliteindemo;
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 {
private static final String DATABASE_NAME = "salaryDatabase.db";
private static final String TABLE_NAME = "SalaryDetails";
private static final int DATABASE_VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_NAME + " (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, " +
"salary TEXT)";
db.execSQL(sql);
}
@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 = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("salary", salary);
return db.insert(TABLE_NAME, null, values) != -1;
}
// IN ऑपरेटर: वेतन 10 या 10000 है
public ArrayList<String> getAllContacts() {
ArrayList<String> list = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT name FROM " + TABLE_NAME +
" WHERE salary IN ('10', '10000')";
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
} while (cursor.moveToNext());
}
cursor.close();
return list;
}
}
IN ऑपरेटर समझाया गया
SQL सिंटैक्स:
WHERE column IN (value1, value2, value3, ...)
- निर्दिष्ट मानों में से किसी एक से मेल खाता है
column = value1 OR column = value2 OR ...का संक्षिप्त रूप- संख्या, स्ट्रिंग, तारीख के साथ काम करता है
- सबक्वेरी के साथ उपयोग किया जा सकता है:
WHERE id IN (SELECT id FROM other_table)
IN बनाम NOT IN
| ऑपरेटर | विवरण | उदाहरण |
|---|---|---|
IN |
सूची में किसी मान से मेल खाता है | salary IN ('10', '10000') |
NOT IN |
सूची में कोई मान नहीं मिलाता | salary NOT IN ('10', '10000') |
ऐप चलाएं
वेतन 10, 10000 और अन्य मानों के साथ रिकॉर्ड जोड़ें। "रिफ्रेश" पर क्लिक करके केवल वेतन 10 या 10000 वाले नाम देखें।
सर्वोत्तम अभ्यास: बड़ी सूचियों के लिए, डायनामिक IN क्लॉज बनाने के लिए पैरामीटराइज़्ड क्वेरी या अस्थायी टेबल का उपयोग करें।