Sqlite Database
Sqlite Database
INTRODUCTION TO
DATABASES
BY DARSHAN SIR
V2V EdTech LLP
Database Basics
Introduction to databases in Android development.
In Android development, databases are essential for storing and managing structured data
locally on the device. They provide a way to persist user data, cache network responses, and
manage application state across sessions.
Android offers several database solutions, ranging from built-in options like SQLite to third-
party libraries that simplify database interactions.
Persistent Storage
Efficient Data Management
Optimized Data Access:
Data Integrity and Security
V2V EdTech LLP
Database Basics
Overview of SQLite as the built-in relational database.
SQLite is the default relational database management system (RDBMS) embedded within Android.
It is a lightweight, self-contained, serverless, zero-configuration, and transactional SQL database
engine.
SQLite is extensively used in Android applications for managing and storing local data efficiently.
V2V EdTech LLP
Database Basics
Overview of SQLite as the built-in relational database.
Database Basics
Overview of SQLite as the built-in relational database.
SQLite in Android
Understanding SQLiteOpenHelper and SQLiteDatabase.
SQLiteOpenHelper
SQLite in Android
Understanding SQLiteOpenHelper and SQLiteDatabase.
SQLite in Android
Understanding SQLiteOpenHelper and SQLiteDatabase.
SQLiteDatabase db = dbHelper.getReadableDatabase();
V2V EdTech LLP
SQLite in Android
Understanding SQLiteOpenHelper and SQLiteDatabase.
CODE
SQLiteDatabase db = dbHelper.getWritableDatabase();
V2V EdTech LLP
SQLite in Android
Understanding SQLiteOpenHelper and SQLiteDatabase.
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS users;");
onCreate(db);
}
}
V2V EdTech LLP
SQLite in Android
Understanding SQLiteOpenHelper and SQLiteDatabase.
SQLiteDatabase
SQLiteDatabase represents the database itself and provides methods to perform database operations
such as querying, inserting, updating, and deleting data.
1. execSQL: Executes a single SQL statement that does not return data, such as an INSERT, UPDATE, or
DELETE.
CODE
SQLite in Android
Understanding SQLiteOpenHelper and SQLiteDatabase.
Key Methods of SQLiteDatabase
2. query: Queries the database and returns a Cursor over the result set..
CODE
Cursor cursor = db.query(
"users", // Table name
null, // Columns to return (null for all)
null, // WHERE clause
null, // WHERE arguments
null, // GROUP BY clause
null, // HAVING clause
null // ORDER BY clause
);
V2V EdTech LLP
SQLite in Android
Understanding SQLiteOpenHelper and SQLiteDatabase.
Key Methods of SQLiteDatabase
3. insert: Inserts a new row into the database and returns the row ID of the newly inserted row.
CODE
ContentValues values = new ContentValues();
values.put("name", "John");
values.put("age", 30);
long newRowId = db.insert("users", null, values);
4. update: Updates rows in the database and returns the number of rows affected.
CODE
ContentValues values = new ContentValues();
values.put("age", 35);
String selection = "name = ?";
String[] selectionArgs = { "John" };
int count = db.update("users", values, selection, selectionArgs);
V2V EdTech LLP
SQLite in Android
Understanding SQLiteOpenHelper and SQLiteDatabase.
Key Methods of SQLiteDatabase
5. delete: Deletes rows from the database and returns the number of rows deleted.
CODE
String selection = "name LIKE ?";
String[] selectionArgs = { "John" };
int deletedRows = db.delete("users", selection, selectionArgs);
V2V EdTech LLP
SQLite in Android
Understanding SQLiteOpenHelper and SQLiteDatabase.
Example of Using SQLiteDatabase with SQLiteOpenHelper
V2V EdTech LLP
SQLite in Android
Creating, Opening, and Upgrading a SQLite Database in Android
Managing a SQLite database in an Android application involves creating the database, opening it
for operations, and handling schema upgrades. This process is facilitated by SQLiteOpenHelper
and SQLiteDatabase.
Example
V2V EdTech LLP
SQLite in Android
Executing SQL queries for CRUD operations.
To execute SQL queries for CRUD (Create, Read, Update, Delete) operations in Android, you will use
the SQLiteDatabase class.
Example
V2V EdTech LLP
Example
V2V EdTech LLP
THANK YOU