Govur University Logo
--> --> --> -->
...

Discuss the basics of database integration in Android. How can you use SQLite to store and retrieve data in your applications?



Database integration is an essential aspect of Android app development as it allows you to store, retrieve, and manage structured data within your applications. Android provides built-in support for SQLite, a lightweight and efficient relational database management system (RDBMS). SQLite is a popular choice for mobile app development due to its small footprint, simplicity, and compatibility with Android devices. Let's explore the basics of database integration in Android and how you can use SQLite to store and retrieve data in your applications.

1. Setting up the Database:
To integrate a database in your Android app, you need to follow these basic steps:

a. Define a Database Helper Class:
Create a class that extends the SQLiteOpenHelper class. This class will handle database creation, version management, and other related tasks. You will override methods such as onCreate() and onUpgrade() to define the database schema and any necessary migrations.

b. Create a Database Instance:
Instantiate the database helper class and create a database instance using the getWritableDatabase() or getReadableDatabase() methods. These methods will handle the creation or opening of the database file.
2. Defining the Database Schema:
The database schema defines the structure of the database, including tables, columns, and their data types. You can define the schema using SQL statements within the onCreate() method of your database helper class. For example, to create a table for storing user data, you can use the following SQL statement:

```
java`private static final String CREATE\_TABLE\_USERS = "CREATE TABLE " + TABLE_USERS +
"(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_EMAIL + " TEXT)";`
```
3. Performing Database Operations:
Once the database is set up and the schema is defined, you can perform various operations such as inserting, updating, deleting, and querying data.

a. Inserting Data:
To insert data into the database, you can use the insert() method provided by the SQLiteDatabase class. This method takes the table name, column-value pairs, and returns the row ID of the newly inserted record. For example:

```
java`ContentValues values = new ContentValues();
values.put(COLUMN_NAME, "John Doe");
values.put(COLUMN_EMAIL, "john@example.com");

long rowId = db.insert(TABLE_USERS, null, values);`
```
b. Updating Data:
To update existing data in the database, you can use the update() method. This method takes the table name, column-value pairs, selection criteria, and returns the number of rows affected. For example:

```
java`ContentValues values = new ContentValues();
values.put(COLUMN_NAME, "Updated Name");

String selection = COLUMN_ID + " = ?";
String[] selectionArgs = {String.valueOf(rowId)};

int rowsAffected = db.update(TABLE_USERS, values, selection, selectionArgs);`
```
c. Querying Data:
To retrieve data from the database, you can use the query() method. This method takes the table name, columns to retrieve, selection criteria, and returns a Cursor object containing the query results. You can then iterate over the cursor to access the retrieved data. For example:

```
java`String[] projection = {COLUMN_ID, COLUMN_NAME, COLUMN_EMAIL};
String selection = COLUMN_NAME + " = ?";
String[] selectionArgs = {"John Doe"};

Cursor cursor = db.query(TABLE_USERS, projection, selection, selectionArgs, null, null, null);

if (cursor.moveToFirst()) {
do {
long id = cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NAME));
String email = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_EMAIL));

// Process the retrieved data
} while (cursor.moveToNext());
}

cursor`
```