0% found this document useful (0 votes)
16 views21 pages

Sqlite Database

The document provides an introduction to databases in Android development, emphasizing the importance of persistent storage, efficient data management, and data integrity. It focuses on SQLite as the built-in relational database, detailing its key features and the role of SQLiteOpenHelper and SQLiteDatabase classes for managing database operations. The document also covers CRUD operations and the implementation of a database helper class for effective database management.

Uploaded by

jk97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views21 pages

Sqlite Database

The document provides an introduction to databases in Android development, emphasizing the importance of persistent storage, efficient data management, and data integrity. It focuses on SQLite as the built-in relational database, detailing its key features and the role of SQLiteOpenHelper and SQLiteDatabase classes for managing database operations. The document also covers CRUD operations and the implementation of a database helper class for effective database management.

Uploaded by

jk97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

V2V EdTech LLP

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.

Need of using database

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.

Key Features of SQLite


1. Self-Contained: SQLite is a complete database system that resides within the application, requiring
no external dependencies.
2. Zero Configuration: Unlike other database systems, SQLite does not require any configuration or
administration, making it easy to set up and use.
3. Serverless: SQLite is serverless, meaning it does not require a separate server process, simplifying its
use and reducing overhead.
4. Transactional: SQLite supports ACID (Atomicity, Consistency, Isolation, Durability) transactions,
ensuring reliable and consistent data operations.
5. Cross-Platform: Although it is primarily used in Android, SQLite is cross-platform and can be used in
various operating systems and environments.
6. SQL Syntax: SQLite uses standard SQL syntax, making it familiar to developers who have experience
with other SQL databases.
V2V EdTech LLP

Database Basics
Overview of SQLite as the built-in relational database.

Key Features of SQLite


Self-Contained: SQLite is a complete database system that resides within the application, requiring
no external dependencies.
Zero Configuration: Unlike other database systems, SQLite does not require any configuration or
administration, making it easy to set up and use.
Serverless: SQLite is serverless, meaning it does not require a separate server process, simplifying its
use and reducing overhead.
Transactional: SQLite supports ACID (Atomicity, Consistency, Isolation, Durability) transactions,
ensuring reliable and consistent data operations.
Cross-Platform: Although it is primarily used in Android, SQLite is cross-platform and can be used in
various operating systems and environments.
SQL Syntax: SQLite uses standard SQL syntax, making it familiar to developers who have experience
with other SQL databases.
V2V EdTech LLP

SQLite in Android
Understanding SQLiteOpenHelper and SQLiteDatabase.

In Android development, SQLiteOpenHelper and SQLiteDatabase are essential classes that


facilitate working with SQLite databases. They help manage database creation, version
management, and perform CRUD (Create, Read, Update, Delete) operations.

SQLiteOpenHelper

SQLiteOpenHelper is an abstract class that simplifies database creation and management. It


provides two primary methods that you must override:
onCreate(SQLiteDatabase db): Called when the database is created for the first time. This is
where you define the initial database schema.
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion): Called when the database needs
to be upgraded. This is where you handle schema changes and data migration between versions.
V2V EdTech LLP

SQLite in Android
Understanding SQLiteOpenHelper and SQLiteDatabase.

Key Methods of SQLiteOpenHelper

1. Constructor: Initializes the database helper.


CODE
public MyDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

2. onCreate: Defines the database schema and initial data.


CODE
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age
INTEGER);");
}
V2V EdTech LLP

SQLite in Android
Understanding SQLiteOpenHelper and SQLiteDatabase.

Key Methods of SQLiteOpenHelper

3. onUpgrade: Handles database schema changes and data migration.


CODE
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS users;");
onCreate(db);
}

4. getReadableDatabase(): Opens the database in read-only mode.


CODE

SQLiteDatabase db = dbHelper.getReadableDatabase();
V2V EdTech LLP

SQLite in Android
Understanding SQLiteOpenHelper and SQLiteDatabase.

Key Methods of SQLiteOpenHelper

5. getWritableDatabase(): Opens the database in read/write mode.

CODE

SQLiteDatabase db = dbHelper.getWritableDatabase();
V2V EdTech LLP

SQLite in Android
Understanding SQLiteOpenHelper and SQLiteDatabase.

Example of SQLiteOpenHelper Implementation


public class MyDatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "mydatabase.db";


private static final int DATABASE_VERSION = 1;

public MyDatabaseHelper(Context context) {


super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@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.

Key Methods of SQLiteDatabase

1. execSQL: Executes a single SQL statement that does not return data, such as an INSERT, UPDATE, or
DELETE.

CODE

db.execSQL("INSERT INTO users (name, age) VALUES ('John', 30);");


V2V EdTech LLP

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

Working with SQLite


Performing CRUD operations (Create, Read, Update, Delete) with SQLite.
Performing CRUD operations (Create, Read, Update, Delete) with SQLite in Android involves using
the SQLiteOpenHelper class to manage database creation and versioning, and the SQLiteDatabase
class to execute SQL commands.
Example
V2V EdTech LLP

Working with SQLite


Using SQLiteOpenHelper to manage database creation and versioning.
The SQLiteOpenHelper class in Android provides a way to manage database creation and
versioning. By extending this class, you can handle database creation, upgrades, and version
control.

1. Create a Database Helper Class


2. Implement the onCreate Method
3. Implement the onUpgrade Method
4. Using the Database Helper in Your Application
V2V EdTech LLP

Working with SQLite


Using SQLiteOpenHelper to manage database creation and versioning.

Example
V2V EdTech LLP

THANK YOU

You might also like