एंड्रॉइड SQLite में LIKE ऑपरेटर का उपयोग
यह ट्यूटोरियल पैटर्न मिलान के लिए SQLite में LIKE ऑपरेटर का उपयोग करने का तरीका दिखाता है।
चरण 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.sqlitelikedemo;
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;
}
// LIKE ऑपरेटर: नाम 'Sa' से शुरू होता है
public ArrayList<String> getAllContacts() {
ArrayList<String> list = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT name FROM " + TABLE_NAME +
" WHERE name LIKE 'Sa%'";
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
} while (cursor.moveToNext());
}
cursor.close();
return list;
}
}
LIKE ऑपरेटर वाइल्डकार्ड
| वाइल्डकार्ड | विवरण | उदाहरण | मिलान |
|---|---|---|---|
% |
शून्य या अधिक वर्ण | 'Sa%' |
"Sa" से शुरू होने वाला कोई भी नाम |
_ |
एकल वर्ण | 'S_m' |
Sam, Sum, S4m, आदि |
%text% |
कहीं भी 'text' शामिल है | '%kumar%' |
कुमार, कुमारस्वामी, आदि |
केस संवेदनशीलता
- डिफ़ॉल्ट: ASCII वर्णों के लिए केस-असंवेदनशील
PRAGMA case_sensitive_like = ON;केस-संवेदनशील के लिए- यूनिकोड के लिए
COLLATE NOCASEका उपयोग करें
ऐप चलाएं
"Sa" से शुरू होने वाले नाम जोड़ें (साई, संजय, सारा) और कुछ अन्य। "रिफ्रेश" पर क्लिक करके केवल "Sa" से शुरू होने वाले नाम देखें।
प्रदर्शन नोट: अग्रणी वाइल्डकार्ड (%text) के साथ LIKE इंडेक्स का उपयोग नहीं कर सकता। बड़े डेटासेट के लिए फुल-टेक्स्ट सर्च (FTS) का उपयोग करने पर विचार करें।