Android_Programming(Unit-)
Android_Programming(Unit-)
1. SQLite in Android
SQLite is a lightweight, relational database embedded in Android. It is used for local data storage in
mobile applications.
• Example:
@Override
String CREATE_TABLE = "CREATE TABLE MyTable (id INTEGER PRIMARY KEY, name TEXT, age
INTEGER)";
db.execSQL(CREATE_TABLE);
@Override
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)
SQLiteDatabase db = dbHelper.getWritableDatabase();
values.put("name", "John");
values.put("age", 25);
values.put("age", 30);
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"));
Log.d("Data", "ID: " + id + ", Name: " + name + ", Age: " + age);
cursor.close();
5. Content Providers
Content Providers enable applications to share data with other apps securely. They act as an
abstraction layer over the database.
• Implement the required methods (onCreate, query, insert, update, delete, getType).
@Override
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();
@Nullable
@Override
SQLiteDatabase db = dbHelper.getWritableDatabase();
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection,
@Nullable String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
@Nullable
@Override
return "vnd.android.cursor.dir/vnd.com.example.mytable";
Querying Data
Cursor cursor =
getContentResolver().query(Uri.parse("content://com.example.myprovider/MyTable"), null, null,
null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
}
Android Programming(Unit-IV)
cursor.close();
Inserting Data
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).
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.
@Override
db.execSQL("CREATE TABLE Users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)");
@Override
Android Programming(Unit-IV)
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:
• values.put("name", "Alice");
• values.put("age", 25);
• Updating a Row:
If you want to change Alice's age:
• values.put("age", 30);
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.
• Cursor cursor = db.query("Users", null, "age>?", new String[]{"20"}, null, null, "name ASC");
• while (cursor.moveToNext()) {
• }
• cursor.close();
• Adding Data: Use insert() with ContentValues to save new rows in the database.