0% found this document useful (0 votes)
1 views3 pages

1 Launching The Application in Android Studio

Android uses SQLite as an embedded relational database for local data storage, supporting ACID properties and CRUD operations. To create a database, developers extend the SQLiteOpenHelper class, implementing methods for database creation and upgrades. SQLite offers advantages such as being lightweight, fast, and requiring no installation, ensuring persistent and secure data storage.

Uploaded by

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

1 Launching The Application in Android Studio

Android uses SQLite as an embedded relational database for local data storage, supporting ACID properties and CRUD operations. To create a database, developers extend the SQLiteOpenHelper class, implementing methods for database creation and upgrades. SQLite offers advantages such as being lightweight, fast, and requiring no installation, ensuring persistent and secure data storage.

Uploaded by

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

📚 Creating or Using Databases in Android – Theory

🔹 Introduction
Android provides multiple options to store data locally on a device. When
structured, persistent storage is required, databases are the most efficient
choice. Android uses SQLite, a lightweight, embedded relational database
engine, for database operations.

🔹 SQLite Database in Android


SQLite is an open-source, embedded SQL database used in Android for local data
storage. It supports ACID (Atomicity, Consistency, Isolation, Durability)
properties and allows CRUD (Create, Read, Update, Delete) operations using SQL
syntax.

🔹 Creating a Database
To create and manage a SQLite database in Android, we extend the
SQLiteOpenHelper class which provides two important lifecycle methods:
1. onCreate(SQLiteDatabase db)
Called when the database is created for the first time. Used to create
tables and initialize data.
2. onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
Called when the database version is incremented. Used to alter tables,
drop old ones, or migrate data.

🔹 SQLiteOpenHelper Class – Structure


java
CopyEdit
public class MyDatabaseHelper extends SQLiteOpenHelper {
public MyDatabaseHelper(Context context) {
super(context, "MyDatabase.db", null, 1); // DB name and version
}

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

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

🔹 CRUD Operations
 Insert Data:
java
CopyEdit
ContentValues values = new ContentValues();
values.put("name", "Alice");
db.insert("users", null, values);
 Read Data:
java
CopyEdit
Cursor cursor = db.rawQuery("SELECT * FROM users", null);
 Update Data:
java
CopyEdit
ContentValues values = new ContentValues();
values.put("name", "Bob");
db.update("users", values, "id=?", new String[]{"1"});
 Delete Data:
java
CopyEdit
db.delete("users", "id=?", new String[]{"1"});

🔹 Advantages of SQLite in Android


 No need to install or configure
 Lightweight and fast
 Fully compatible with SQL
 Local, secure, and private
 Persistent data even after app is closed

You might also like