SQLite में typeof() फंक्शन को समझना
उदाहरण में जाने से पहले, Android में SQLite डेटाबेस क्या है, यह जानना जरूरी है। SQLite एक ओपन-सोर्स SQL डेटाबेस है जो डिवाइस पर टेक्स्ट फ़ाइल में डेटा स्टोर करता है। Android में SQLite का बिल्ट-इन इम्प्लीमेंटेशन होता है।
SQLite का typeof() फंक्शन किसी कॉलम या एक्सप्रेशन के डेटा टाइप को स्ट्रिंग के रूप में लौटाता है। संभावित मान हैं: null, integer, real, text, blob।
चरण 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 - typeof() का उपयोग
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;
}
// typeof() फंक्शन का उपयोग करके नाम कॉलम का टाइप प्राप्त करें
public ArrayList<String> getAllContacts() {
SQLiteDatabase db = getReadableDatabase();
ArrayList<String> list = new ArrayList<>();
// typeof(name) - नाम कॉलम का डेटा टाइप लौटाएगा (text)
Cursor cursor = db.rawQuery("SELECT typeof(name) 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;
}
}
ऐप चलाएं
डिवाइस कनेक्ट करें और Android Studio से ऐप चलाएं।

परिणाम में, यह दिखा रहा है कि नाम का टाइप क्या है। हमारे मामले में यह TEXT है, इसलिए परिणाम "text" दिखाता है। typeof() फंक्शन डिबगिंग और डेटा वैलिडेशन के लिए उपयोगी है।