SQLite is a lightweight relational database engine built into Android, ideal for local data persistence. The android.database.sqlite package provides everything you need without additional setup.
SQLiteOpenHelper Basics
To create and open a database, extend SQLiteOpenHelper. The constructor takes a database name and version number. The system checks if the database already exists — if so, it opens it; otherwise, it creates a new one and calls onCreate.
| |
Note that the database is not created or opened until getWritableDatabase() or getReadableDatabase() is called. onUpgrade is triggered when the version number increases — handle schema migrations here.
Singleton Pattern and Thread Safety
A SQLiteOpenHelper subclass returns the same SQLiteDatabase instance. This means calling close() from any thread closes all database instances in your app. Be mindful of open/close timing.
Best practices:
- Use a singleton pattern to manage your
SQLiteOpenHelperinstance - Avoid opening and closing database connections frequently across different parts of your app
- Unless data volume is enormous, consolidate data into a single database rather than managing multiple
SQLiteOpenHelperinstances
File Location and Security
Database files are private to your app, stored at /data/data/(packageName)/database/. However:
- The database file is not encrypted — anyone with root access can read it
- To export the database on a non-rooted device, copy it to a public location from your app
- For sensitive data, consider SQLCipher or Android’s
EncryptedDatabase