Android Programming(Unit-IV)
1. SQLite in Android
SQLite is a lightweight, relational database embedded in Android. It is used for local data storage in
mobile applications.
Creating an SQLite Database
• Extend the SQLiteOpenHelper class.
• Override the onCreate() and onUpgrade() methods.
• Example:
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) {
String CREATE_TABLE = "CREATE TABLE MyTable (id INTEGER PRIMARY KEY, name TEXT, age
INTEGER)";
db.execSQL(CREATE_TABLE);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS MyTable");
onCreate(db);
2. ContentValues
ContentValues is used to store a set of key-value pairs to insert or update data in the SQLite
database.
Android Programming(Unit-IV)
Example: Adding a Row
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "John");
values.put("age", 25);
db.insert("MyTable", null, values);
Example: Updating a Row
ContentValues values = new ContentValues();
values.put("age", 30);
db.update("MyTable", values, "name=?", new String[]{"John"});
3. Cursors
A Cursor provides access to the results of a database query. It allows you to navigate and retrieve
data.
Querying a Database
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("MyTable", null, "age>?", new String[]{"20"}, null, null, "name ASC");
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Log.d("Data", "ID: " + id + ", Name: " + name + ", Age: " + age);
cursor.close();
4. Adding, Updating, and Removing Rows
• Adding Rows: Use db.insert().
• Updating Rows: Use db.update().
• Removing Rows: Use db.delete().
Example: Deleting a Row
Android Programming(Unit-IV)
db.delete("MyTable", "name=?", new String[]{"John"});
5. Content Providers
Content Providers enable applications to share data with other apps securely. They act as an
abstraction layer over the database.
Creating a Content Provider
• Extend the ContentProvider class.
• Implement the required methods (onCreate, query, insert, update, delete, getType).
Example: ContentProvider Implementation
public class MyContentProvider extends ContentProvider {
private MyDatabaseHelper dbHelper;
@Override
public boolean onCreate() {
dbHelper = new MyDatabaseHelper(getContext());
return true;
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,
@Nullable String[] selectionArgs, @Nullable String sortOrder) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
return db.query("MyTable", projection, selection, selectionArgs, null, null, sortOrder);
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
long id = db.insert("MyTable", null, values);
return ContentUris.withAppendedId(uri, id);
Android Programming(Unit-IV)
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection,
@Nullable String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
return db.update("MyTable", values, selection, selectionArgs);
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
return db.delete("MyTable", selection, selectionArgs);
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return "vnd.android.cursor.dir/vnd.com.example.mytable";
6. Using a Content Provider
Querying Data
Cursor cursor =
getContentResolver().query(Uri.parse("content://com.example.myprovider/MyTable"), null, null,
null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
Log.d("Data", "Name: " + name);
}
Android Programming(Unit-IV)
cursor.close();
Inserting Data
ContentValues values = new ContentValues();
values.put("name", "Jane");
values.put("age", 28);
getContentResolver().insert(Uri.parse("content://com.example.myprovider/MyTable"), values);
SQLite in Android
SQLite is a built-in database in Android that helps you store data directly on the device. It's like
having a mini-database that you can use to save things like user details, app settings, or offline
content. For example, if you're building a notes app, you can use SQLite to store the notes locally on
the phone.
To create and manage an SQLite database, you use a helper class called SQLiteOpenHelper. It
simplifies the process of creating the database and managing updates when you need to make
changes to the structure (like adding a new column).
Creating an SQLite Database
You first create a class that extends SQLiteOpenHelper. Inside this class, you define what the
database should look like (e.g., tables and columns) and how it should behave when it's created or
updated.
Here’s a simple example:
public class MyDatabaseHelper extends SQLiteOpenHelper {
public MyDatabaseHelper(Context context) {
super(context, "MyDatabase.db", null, 1);
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE Users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)");
@Override
Android Programming(Unit-IV)
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS Users");
onCreate(db);
• The onCreate() method runs when the database is created for the first time.
• The onUpgrade() method runs if the database version changes, letting you handle updates.
ContentValues
ContentValues is like a container for data. It stores information you want to insert or update in the
database in a key-value format (like a dictionary).
• Adding a Row:
If you want to save a user’s name and age, you use ContentValues like this:
• ContentValues values = new ContentValues();
• values.put("name", "Alice");
• values.put("age", 25);
• db.insert("Users", null, values);
• Updating a Row:
If you want to change Alice's age:
• ContentValues values = new ContentValues();
• values.put("age", 30);
• db.update("Users", values, "name=?", new String[]{"Alice"});
Cursors
A Cursor is how you read data from the database. Think of it as a pointer that moves through rows of
data retrieved from the database. You can use it to get the values from each row.
• Querying the Database:
Let’s say you want to get all users older than 20:
• Cursor cursor = db.query("Users", null, "age>?", new String[]{"20"}, null, null, "name ASC");
• while (cursor.moveToNext()) {
• String name = cursor.getString(cursor.getColumnIndex("name"));
• int age = cursor.getInt(cursor.getColumnIndex("age"));
• Log.d("User", "Name: " + name + ", Age: " + age);
Android Programming(Unit-IV)
• }
• cursor.close();
Adding, Updating, and Removing Rows
• Adding Data: Use insert() with ContentValues to save new rows in the database.
• db.insert("Users", null, values);
• Updating Data: Use update() to modify existing rows.
• db.update("Users", values, "id=?", new String[]{"1"});
• Deleting Data: Use delete() to remove rows from the table.
• db.delete("Users", "name=?", new String[]{"Alice"});