Examples of SQLite in Android usage in production apps?

SQLite is widely used in production Android apps for local data storage. It provides a lightweight, self-contained, serverless, and transactional SQL database engine. Below are some examples of how SQLite can be implemented in Android applications.

Example of SQLite in Android App

// SQLiteOpenHelper class to manage database creation and version management public class MyDatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "mydatabase.db"; private static final int DATABASE_VERSION = 1; public static final String TABLE_NAME = "users"; public static final String COLUMN_ID = "id"; public static final String COLUMN_NAME = "name"; public static final String COLUMN_AGE = "age"; public MyDatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_NAME + " TEXT," + COLUMN_AGE + " INTEGER" + ")"; db.execSQL(CREATE_USERS_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } // Method to add a new user public void addUser(String name, int age) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_NAME, name); values.put(COLUMN_AGE, age); db.insert(TABLE_NAME, null, values); db.close(); } // Method to fetch all users public Cursor getAllUsers() { SQLiteDatabase db = this.getReadableDatabase(); return db.rawQuery("SELECT * FROM " + TABLE_NAME, null); } }

SQLite Android Database Local Storage SQLiteOpenHelper ContentValues