Theory
Data जो App बंद करने के बाद भी बचता है
FestConnect Mobile registrations लेता है, पर जब user app बंद करता है, ये गायब हो जाते हैं: सब कुछ memory में था। एक real app REMEMBER करता है।
Android हर device में exactly इसके लिए एक database ship करता है: SQLite, एक lightweight relational database जो data locally phone पर store करता है, कोई server नहीं चाहिए। आप BCA205 और BCA303 से SQL already जानते हैं; SQLite वही SQL है, device पर चलता हुआ। यह lesson FestConnect Mobile को persistent storage देता है: एक table create करना, और full CRUD (insert, read, update, delete) ताकि registrations sessions के बीच survive करें। (Slug MySQL भी mention करता है: वह एक SERVER database है जो एक app network पर एक API के through पहुँचता है; SQLite यहाँ on-device choice है।)
Theory
SQLite और SQLiteOpenHelper
SQLite Android के अंदर एक embedded relational database है, device पर एक file की तरह stored। इसे इस्तेमाल करने के लिए, आप SQLiteOpenHelper को extend करने वाली एक helper class create करते हैं, जो database की creation और versioning manage करती है:
- onCreate(db): सिर्फ़ ONCE चलता है जब database पहली बार create होता है: यहाँ अपना
CREATE TABLESQL डालिए - onUpgrade(db, old, new): schema changes handle करता है जब आप database version bump करते हैं
Helper आपको operations चलाने के लिए एक SQLiteDatabase object देता है। चूँकि यह local और serverless है, SQLite एक phone के लिए perfect है: fast, offline-capable, और app के लिए private। (Registrations या एक cache जैसा on-device data यहाँ रहता है; users के across shared data एक SERVER पर रहता है, एक API के through पहुँचा जाता है।)
Practical
एक Table Create कीजिए और CRUD कीजिए
class DbHelper(context: Context) :
SQLiteOpenHelper(context, "fest.db", null, 1) {
override fun onCreate(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE regs(id INTEGER PRIMARY KEY, name TEXT, event TEXT)")
}
override fun onUpgrade(db: SQLiteDatabase, old: Int, new: Int) {
db.execSQL("DROP TABLE IF EXISTS regs")
onCreate(db)
}
}
// INSERT with ContentValues:
fun addReg(db: SQLiteDatabase, name: String, event: String) {
val values = ContentValues().apply {
put("name", name)
put("event", event)
}
db.insert("regs", null, values)
}
// READ with a parameterized query (injection-safe):
fun findByEvent(db: SQLiteDatabase, event: String): List<String> {
val names = mutableListOf<String>()
val cursor = db.rawQuery("SELECT name FROM regs WHERE event = ?", arrayOf(event))
while (cursor.moveToNext()) {
names.add(cursor.getString(0))
}
cursor.close() // always close the cursor
return names
}
Theory
CRUD, Cursors, और Safety
SQLiteDatabase पर चार operations:
- insert:
db.insert(table, null, contentValues), जहाँ ContentValues column से value का एक key-value है - read:
db.query(...)याdb.rawQuery(sql, args)एक Cursor return करते हैं जिसे आप iterate करते हैं (cursor.moveToNext(),cursor.getString(index)), फिर CLOSE करते हैं - update:
db.update(table, values, whereClause, whereArgs) - delete:
db.delete(table, whereClause, whereArgs)
एक safety rule BCA504 से carry over होता है: SQL injection रोकने के लिए parameterized queries इस्तेमाल कीजिए (? placeholders selectionArgs/arrayOf(...) के साथ), string-concatenated user input KABHI नहीं। SQLite एक real SQL database है, तो इसमें same injection risk और same defence है। (Modern Android अक्सर Room इस्तेमाल करता है, एक library जो SQLite को कम boilerplate से wrap करती है; जानिए यह exist करती है।)
Quiz
SQLite FestConnect Mobile का registration data कहाँ store करता है, और यह एक phone app के लिए suitable क्या बनाता है?
- एक remote server पर, हर read के लिए internet चाहते हुए
- Locally DEVICE पर, एक embedded database की तरह किसी server की ज़रूरत नहीं: fast, offline-capable, और app के लिए private
- सिर्फ़ cloud में
- यह data persistently store नहीं कर सकता
Show the answer
Locally DEVICE पर, एक embedded database की तरह किसी server की ज़रूरत नहीं: fast, offline-capable, और app के लिए private
SQLite device पर LOCALLY stored एक EMBEDDED database है (एक file की तरह), किसी server की ज़रूरत नहीं, यही exactly इसे on-device app data के लिए ideal बनाता है: यह fast है, OFFLINE काम करता है, और data को app के लिए private रखता है। Option A एक SERVER database describe करता है (MySQL की तरह एक API के through पहुँचा गया), जो SQLite नहीं है, इसका पूरा point local और serverless होना है। Option C इसे cloud तक limit करता है, on-device का opposite। Option D false है: SQLite app sessions के across data persist करता है (यही वजह है हम इसे इस्तेमाल करते हैं)। तो local registrations app बंद करने के बाद survive करते हैं क्योंकि ये on-device SQLite database में लिखे जाते हैं। Users के across SHARED data के लिए, आप एक API के through एक server database इस्तेमाल करते; local के लिए, SQLite।
Think first
SQLite या एक Server Database (MySQL)?
FestConnect Mobile registrations को on-device SQLite में या एक server MySQL database में (एक API के through, BCA504 के back-end जैसा) store कर सकता था। हर एक कब सही है? फिर tap कीजिए।
Show the answer
उस data के लिए ON-DEVICE SQLite इस्तेमाल कीजिए जो इस user और इस phone के लिए LOCAL है: एक personal cache, offline drafts, app settings, या data जो सिर्फ़ इस user को चाहिए। यह offline काम करता है, fast है, और कोई network नहीं चाहिए। उस data के लिए एक SERVER database (MySQL एक API के through) इस्तेमाल कीजिए जो SHARED या CENTRAL होना चाहिए: registrations जो fest organisers को देखनी हैं, एक user के devices के across synced data, या कुछ भी जिसका source of truth server है। अक्सर apps DONO इस्तेमाल करते हैं: एक local cache/offline store की तरह SQLite, online होने पर एक server database से sync करते हुए। FestConnect के लिए, अगर registrations organisers तक पहुँचनी चाहिए, ये SERVER पर belong करते हैं (SQLite offline entry के लिए इन्हें locally cache कर सकता है, बाद में sync करते हुए)। Decision इस पर निर्भर करता है data किसे चाहिए और यह offline काम करना चाहिए या नहीं: local-and-offline SQLite की तरफ़ point करता है, shared-and-central एक server database की तरफ़।
Watch out
SQLite Traps
String-Concatenating Queries: parameterized queries इस्तेमाल कीजिए (? + args); SQLite में भी injection risk real है (BCA504 का lesson)।
Cursor Close न करना: पढ़ने के बाद हमेशा cursor.close() कीजिए, नहीं तो आप leak करते हैं।
UI Thread पर Heavy DB Work: database operations main thread से off कीजिए, नहीं तो app freeze होता है।
onUpgrade भूलना: schema changes को एक version bump और onUpgrade handling चाहिए।
SQLite (Local) को MySQL (Server) से Confuse करना: SQLite on-device है; एक server DB एक API के through पहुँचा जाता है।
Theory
यह Remember करता है; अब Device Powers
FestConnect Mobile अब SQLite से data locally persist करता है। Unit के आख़िरी दो lessons phone के HARDWARE को tap करते हैं: user की current LOCATION access करना (nearby fest venues दिखाने के लिए), और CAMERA से एक image capture करना (एक profile या event photo के लिए)। ये device capabilities, permissions से gated, वह हैं जो एक mobile app को एक छोटी website से ज़्यादा बनाती हैं। Location अगला, फिर camera।
Summary
Key takeaways
- SQLite Android का built-in, on-device relational database है (एक file, कोई server नहीं): fast, offline, app के लिए private।
- SQLiteOpenHelper extend कीजिए: onCreate CREATE TABLE चलाता है; onUpgrade schema/version changes handle करता है।
- SQLiteDatabase पर CRUD: insert (ContentValues), query/rawQuery (एक Cursor return करते हैं), update, delete।
- moveToNext() और getString(index) से एक Cursor iterate कीजिए; हमेशा इसे close कीजिए।
- Parameterized queries इस्तेमाल कीजिए (? + args), concatenated input कभी नहीं: SQL injection risk BCA504 से carry over होता है।
- SQLite LOCAL data के लिए है; एक server database (API के through MySQL) SHARED/central data के लिए है; Room कम boilerplate से SQLite wrap करता है।
- Memory hook: SQLite on-device SQL database है; helper tables बनाता है, db के through CRUD, queries parameterize कीजिए।