Mad Unit 5
Mad Unit 5
SQLite is a lightweight, relational database management system (RDBMS) embedded within Android.
It is serverless, self-contained, and requires minimal setup, making it ideal for mobile applications.
SQLite supports standard relational database features like SQL syntax, transactions, and joins.
To create and open a database in Android, you typically use the `SQLiteOpenHelper` class. This class
manages database creation and version management.
**Example:**
```java
@Override
db.execSQL("CREATE TABLE mytable (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)");
@Override
onCreate(db);
}
```
```java
SQLiteDatabase db = dbHelper.getWritableDatabase();
```
Tables in SQLite are created using SQL `CREATE TABLE` statements. This is typically done in the
`onCreate` method of your `SQLiteOpenHelper` subclass.
**Example:**
```java
@Override
"age INTEGER)";
db.execSQL(CREATE_TABLE);
```
Inserting data into an SQLite database is performed using the `insert` method of `SQLiteDatabase`.
**Example:**
```java
values.put("age", 25);
```
Retrieving data is done using the `query` method of `SQLiteDatabase` or raw SQL queries.
**Example:**
```java
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("id"));
} while (cursor.moveToNext());
cursor.close();
```
**Example:**
```java
values.put("age", 26);
```
**Example:**
```java
```
A Content Provider manages access to a structured set of data and is the standard interface for data
exchange between applications.
**Example:**
```java
@Override
return true;
}
// Implement query, insert, update, delete methods
```
2. **Declare in AndroidManifest.xml:**
```xml
<provider
android:name=".MyContentProvider"
android:authorities="com.example.mycontentprovider"
android:exported="true"/>
```
**Example:**
```java
values.put("age", 30);
```
**Example:**
```java
if (cursor != null) {
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
cursor.close();
```
**Example:**
```java
values.put("age", 31);
Uri.parse("content://com.example.mycontentprovider/mytable"),
```
**Example:**
```java
Uri.parse("content://com.example.mycontentprovider/mytable"),
By understanding and implementing these concepts, you can effectively manage persistent storage
in Android applications using SQLite databases and Content Providers.