Skip to content
Home » Explain how to create and use databases in SQLite for Android applications

Explain how to create and use databases in SQLite for Android applications

    Introduction

    SQLite is a relational database that is serverless, self-contained, and mostly ACID-comforming; it can store structured data in an android device. It’s among the Android OS goodies baked in ready to save and retrieve data offline.

    Explanation

    Android SQLite operates with the help of SQLiteOpenHelper, a class that handles database creation and version updating. Devs write tables, put stuff in them, edit and delete those entries with SQL queries.

    To use SQLite in Android, generally the process goes like this:

    Create a class extending SQLiteOpenHelper

    Override onCreate() to create tables.

    Overrride onUpgrade() and manage upgrades.

    Code some of the SQL necessary to build tables.

    Example:

    CREATE TABLE students(id INTEGER PRIAMRY KEY AUTOINCREMENT, name TEXT, marks INTEGER);

    Use CRUD operations

    Create – Insert values

    Read – Select records

    Update – Modify records

    Delete – Remove records

    Access the database

    Use getWritableDatabase() or getReadableDatabase().

    Features / Characteristics

    • Small and quick for local storage dumbass.
    • No external server is required
    • Uses standard SQL syntax
    • Supports ACID transactions (Atomicity, Consistency, Isolation, Durability)
    • Stores data in a single. db file

    Great for off-line applications such as note-pad, digital calculator and student record

    Example

    • Simple SQLiteOpenHelper Class Example

    public class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context) {
        super(context, "StudentDB", null, 1);
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE students(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, marks INTEGER)");
    }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS students");
        onCreate(db);
    }

    }

    Insert Data Example:

    SQLiteDatabase db = helper. getWritableDatabase(); ContentValues cv = new ContentValues(); cv. put(“name”, “Charan”); cv. put(“marks”, 90); db. insert(“students”, null, cv);

    Advantages / Importance

    • Provides efficient local storage
    • No need for internet connectivity
    • Fully supported by Android framework
    • Better performance than external databases
    • Quick backup and restore of all data
    • Suitable for medium-sized structured datasets

    Conclusion

    SQLite is a powerful local database that comes pre-installed with Android which offers the capacity to store, manage and manipulate structured data. Its easy installation process along with the ability to work offline and supporting SQL also make it a perfect fit for mobile apps in need of an organized data structure.

    Leave a Reply

    Your email address will not be published. Required fields are marked *