[go: up one dir, main page]

0% found this document useful (0 votes)
9 views7 pages

Mad Unit 5

SQLite is a lightweight, serverless relational database management system embedded in Android, ideal for mobile applications. The document provides a comprehensive guide on creating, opening, and managing SQLite databases, including examples for inserting, retrieving, updating, and deleting data, as well as using Content Providers for data exchange between applications. It emphasizes the use of the SQLiteOpenHelper class for database management and the implementation of standard SQL operations.

Uploaded by

sonusurepally
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)
9 views7 pages

Mad Unit 5

SQLite is a lightweight, serverless relational database management system embedded in Android, ideal for mobile applications. The document provides a comprehensive guide on creating, opening, and managing SQLite databases, including examples for inserting, retrieving, updating, and deleting data, as well as using Content Providers for data exchange between applications. It emphasizes the use of the SQLiteOpenHelper class for database management and the implementation of standard SQL operations.

Uploaded by

sonusurepally
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/ 7

3### Introduction to SQLite Database (5 marks)

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.

### Creating and Opening a Database (5 marks)

To create and open a database in Android, you typically use the `SQLiteOpenHelper` class. This class
manages database creation and version management.

**Example:**

1. **Create a Helper Class:**

```java

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 mytable (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)");

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS mytable");

onCreate(db);
}

```

2. **Open the Database:**

```java

MyDatabaseHelper dbHelper = new MyDatabaseHelper(context);

SQLiteDatabase db = dbHelper.getWritableDatabase();

```

### Creating Tables (5 marks)

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

public void onCreate(SQLiteDatabase db) {

String CREATE_TABLE = "CREATE TABLE mytable (" +

"id INTEGER PRIMARY KEY AUTOINCREMENT, " +

"name TEXT, " +

"age INTEGER)";

db.execSQL(CREATE_TABLE);

```

### Inserting Data (5 marks)

Inserting data into an SQLite database is performed using the `insert` method of `SQLiteDatabase`.
**Example:**

```java

ContentValues values = new ContentValues();

values.put("name", "John Doe");

values.put("age", 25);

db.insert("mytable", null, values);

```

### Retrieving Data (5 marks)

Retrieving data is done using the `query` method of `SQLiteDatabase` or raw SQL queries.

**Example:**

```java

Cursor cursor = db.query("mytable", null, null, null, null, null, null);

if (cursor.moveToFirst()) {

do {

int id = cursor.getInt(cursor.getColumnIndex("id"));

String name = cursor.getString(cursor.getColumnIndex("name"));

int age = cursor.getInt(cursor.getColumnIndex("age"));

// Process the data

} while (cursor.moveToNext());

cursor.close();

```

### Updating Data (5 marks)

Updating data is done using the `update` method of `SQLiteDatabase`.

**Example:**
```java

ContentValues values = new ContentValues();

values.put("age", 26);

db.update("mytable", values, "name = ?", new String[]{"John Doe"});

```

### Deleting Data (5 marks)

Deleting data is done using the `delete` method of `SQLiteDatabase`.

**Example:**

```java

db.delete("mytable", "name = ?", new String[]{"John Doe"});

```

### Registering Content Providers (5 marks)

A Content Provider manages access to a structured set of data and is the standard interface for data
exchange between applications.

**Example:**

1. **Create a Content Provider Class:**

```java

public class MyContentProvider extends ContentProvider {

private MyDatabaseHelper dbHelper;

@Override

public boolean onCreate() {

dbHelper = new MyDatabaseHelper(getContext());

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"/>

```

### Using Content Providers (5 marks each)

#### Insert Data

**Example:**

```java

ContentValues values = new ContentValues();

values.put("name", "Jane Doe");

values.put("age", 30);

Uri newUri = getContentResolver().insert(Uri.parse("content://com.example.mycontentprovider/


mytable"), values);

```

#### Retrieve Data

**Example:**

```java

Cursor cursor = getContentResolver().query(


Uri.parse("content://com.example.mycontentprovider/mytable"),

null, null, null, null);

if (cursor != null) {

while (cursor.moveToNext()) {

int id = cursor.getInt(cursor.getColumnIndex("id"));

String name = cursor.getString(cursor.getColumnIndex("name"));

int age = cursor.getInt(cursor.getColumnIndex("age"));

// Process the data

cursor.close();

```

#### Update Data

**Example:**

```java

ContentValues values = new ContentValues();

values.put("age", 31);

int rowsUpdated = getContentResolver().update(

Uri.parse("content://com.example.mycontentprovider/mytable"),

values, "name = ?", new String[]{"Jane Doe"});

```

#### Delete Data

**Example:**

```java

int rowsDeleted = getContentResolver().delete(

Uri.parse("content://com.example.mycontentprovider/mytable"),

"name = ?", new String[]{"Jane Doe"});


```

By understanding and implementing these concepts, you can effectively manage persistent storage
in Android applications using SQLite databases and Content Providers.

You might also like