View video tutorial

ANDROID SQLite Database

ANDROID

SQLite is the standard and built-in relational database for Android, which is used to store persistent data locally on the user's device.

Android SQLite databases


➔ SQLite databases are an ideal solution for storing data locally in Android applications, requiring no separate server processes or configurations.

➔ SQLite is an in-process library that reads and writes data directly to ordinary disk files located locally on the device.

➔ SQLite for Android does not require any installation, setup, configuration, or administrative procedures, as it is installed by default on Android.

➔ The SQLite library is highly efficient and uses very little memory (typically less than 600 kilobytes), making it suitable for smartphones with limited memory.

➔ It supports transactional (ACID compliant) and atomic, consistent, isolated, and durable (ACID) features, which ensure data integrity even during system crashes or power outages.

➔ SQLite's database file format is binary and is portable across different operating systems (Android, iOS, Windows, Linux).

Create SQLite Database


SQLite is an open source SQL database that stores data in the form of text files on the device's local file system. Like other relational databases, data in the Android SQLite database is stored in the form of tables, where tables are the main source of data. Since SQL is an independent offline local database, SQLite does not need to create a connection to initiate any database operation or action. After creating a database instance, it is possible to perform any operation like INSERT, UPDATE, DELETE, SELECT on the database tables.

Implementing SQLite on Android usually involves creating a helper class to create the database and manage versioning.

Create a database helper class

In Android, a Content URI is a unique identifier used to point to specific data within a Content Provider and acts as a query string for the ContentResolver to perform CRUD (Create, Read, Update, Delete) operations.

The class that extends SQLiteOpenHelper does the work of creating the database and tables.


Define Constants: Defining constants makes it easier to store database names, versions, table names, and column names.

Implement onCreate(): This method runs the first time the database is created, and after the database is created, the table needs to be created using the db.execSQL() method to execute the CREATE TABLE statement.

Implement onUpgrade(): This callback monitors and handles database schema changes, and this method typically drops existing tables and calls onCreate() to recreate them.

Implement CRUD Operations

The following methods can be implemented within a helper class or a separate Data Access Object (DAO) class to perform database create, read, update, delete (CRUD) operations.

Create (insert): This is the insert() method, which takes a ContentValues ​​​​object and stores the data as key-value pairs, where the keys are the column names.

Read (query): This is the query() or rawQuery() method, which returns a Cursor object and iterates through it to extract data.

Update: This is the update() method, which returns new ContentValues ​​and uses a WHERE clause to target specific rows.

Delete: This is the delete() method with a WHERE clause, which is used to delete specific records.