Android में SQLite डेटाबेस
SQLite एक ओपन-सोर्स SQL डेटाबेस है जो डेटा को डिवाइस पर टेक्स्ट फ़ाइल में स्टोर करता है। Android में बिल्ट-इन SQLite इम्प्लीमेंटेशन आता है। यह सभी रिलेशनल डेटाबेस फीचर्स सपोर्ट करता है। इसे एक्सेस करने के लिए JDBC/ODBC जैसे कनेक्शन की आवश्यकता नहीं होती।
उदाहरण: IS NOT NULL क्लॉज का उपयोग
यह उदाहरण दर्शाता है कि SQLite क्वेरी में IS NOT NULL क्लॉज का उपयोग करके नल मानों को कैसे फ़िल्टर किया जाए।
चरण 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" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
नाम और वेतन के लिए एडिटटेक्स्ट, सेव/रिफ्रेश/अपडेट बटन, और डेटा दिखाने के लिए लिस्टव्यू।
चरण 3: MainActivity.java
package com.example.sqlitenotnull;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Button save, refresh;
EditText name, salary;
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 arrayList = helper.getAllContacts();
name = findViewById(R.id.name);
salary = findViewById(R.id.salary);
listView = findViewById(R.id.listView);
final ArrayAdapter arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
findViewById(R.id.refresh).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arrayList.clear();
arrayList.addAll(helper.getAllContacts());
arrayAdapter.notifyDataSetChanged();
listView.invalidateViews();
listView.refreshDrawableState();
}
});
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String nameStr = name.getText().toString();
String salaryStr = salary.getText().toString();
if (!nameStr.isEmpty() && !salaryStr.isEmpty()) {
if (helper.insert(nameStr, salaryStr)) {
Toast.makeText(MainActivity.this, "इन्सर्ट किया गया", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "इन्सर्ट नहीं हुआ", Toast.LENGTH_LONG).show();
}
} else {
name.setError("नाम दर्ज करें");
salary.setError("वेतन दर्ज करें");
}
}
});
}
}
चरण 4: DatabaseHelper.java
package com.example.sqlitenotnull;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.IOException;
import java.util.ArrayList;
class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "salaryDatabase5";
public static final String CONTACTS_TABLE_NAME = "SalaryDetails";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL("CREATE TABLE " + CONTACTS_TABLE_NAME +
"(id INTEGER PRIMARY KEY, name TEXT, salary TEXT, datetime DEFAULT CURRENT_TIMESTAMP)");
} catch (SQLiteException e) {
try {
throw new IOException(e);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + CONTACTS_TABLE_NAME);
onCreate(db);
}
public boolean insert(String name, String salary) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("salary", salary);
db.replace(CONTACTS_TABLE_NAME, null, contentValues);
return true;
}
public ArrayList getAllContacts() {
SQLiteDatabase db = this.getReadableDatabase();
ArrayList arrayList = new ArrayList<>();
// IS NOT NULL क्लॉज का उपयोग
Cursor res = db.rawQuery("SELECT * FROM " + CONTACTS_TABLE_NAME +
" WHERE name = 'sairam' AND name IS NOT NULL", null);
res.moveToFirst();
while (!res.isAfterLast()) {
arrayList.add(res.getString(res.getColumnIndex("name")));
res.moveToNext();
}
return arrayList;
}
public boolean update(String name, String salary) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("UPDATE " + CONTACTS_TABLE_NAME +
" SET name = '" + name + "', salary = '" + salary + "'");
return true;
}
public boolean delete() {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + CONTACTS_TABLE_NAME);
return true;
}
}
ऐप चलाएं
ऐप लॉन्च करें, डेटा एंटर करें, सेव करें, फिर रिफ्रेश दबाएं। केवल वही रिकॉर्ड दिखेंगे जहां नाम 'sairam' है और नल नहीं है।
IS NOT NULL समझाया गया
IS NOT NULLकॉलम में नल मान न होने वाली पंक्तियों को फ़िल्टर करता हैWHERE name = 'sairam' AND name IS NOT NULL- दोनों शर्तें पूरी होनी चाहिए- यह
name != ''से अलग है (खाली स्ट्रिंग ≠ नल) - उपयोगी जब डेटाबेस में नल मान अनुमत हों और आप उन्हें बाहर करना चाहें
मुख्य बिंदु
db.replace()इन्सर्ट या रिप्लेस करता है (प्राइमरी की पर आधारित)- कर्सर इटरेशन के लिए
moveToFirst()औरisAfterLast()उपयोग - रॉ क्वेरी में WHERE क्लॉज के साथ IS NOT NULL
- नोट: अपडेट मेथड में WHERE क्लॉज नहीं है - यह सभी पंक्तियों को अपडेट कर देगा!