Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
36 views

Lecture 7 To 8 For Programming of Mobile Terminals

The document discusses different approaches for local data storage on mobile devices including SharedPreferences, SQLite databases, internal storage, and external storage. It covers how to set up and implement basic CRUD operations for SQLite databases and use SharedPreferences to store key-value pairs of data.

Uploaded by

akaah.randy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lecture 7 To 8 For Programming of Mobile Terminals

The document discusses different approaches for local data storage on mobile devices including SharedPreferences, SQLite databases, internal storage, and external storage. It covers how to set up and implement basic CRUD operations for SQLite databases and use SharedPreferences to store key-value pairs of data.

Uploaded by

akaah.randy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LECTURE 7-8

DATA MANAGEMENT IN MOBILE APPS


Local Data Storage on Mobile Devices

Introduction to Local Data Storage


So, imagine you have an awesome mobile app... but where does it store all the data? Local data
storage on mobile devices is crucial for applications to function smoothly. It allows apps to store
and retrieve information directly on the user's device, without always relying on an internet
connection. This helps in enhancing user experience and making the app more responsive.

Types of Local Data Storage


1. Shared Preferences
What? Shared Preferences allow you to store small amounts of primitive data as key-value pairs.
When to Use? Use Shared Preferences for storing simple data like settings, user preferences, etc.

2. SQLite Databases
What? SQLite is a lightweight database that can be used for storing structured data.
When to Use? Use SQLite when you need to store relational data or larger datasets.

3. Internal Storage
What? Internal storage allows app-specific data to be stored privately on the device.
When to Use? Use Internal Storage for storing private user data like files, databases, etc.

4. External Storage
What? External storage allows data to be stored on the device's external storage, typically an SD
card.
When to Use? Use External Storage for storing larger files or data that can be shared across apps.

5. Room Persistence Library


What? Room is a SQLite object mapping library that provides an abstraction layer over SQLite.
When to Use? Use Room for a more robust and efficient way of working with SQLite databases.

Best Practices for Local Data Storage


- Always encrypt sensitive data to ensure security.
- Handle exceptions and errors gracefully to prevent data loss.
- Be mindful of data storage limits on different devices.
- Implement data syncing mechanisms for seamless user experience across multiple devices.

Conclusion
Local data storage plays a crucial role in mobile app development by enabling apps to store and
manage data locally on the device. Understanding the different types of storage options available
and best practices for implementation is key to creating efficient and user-friendly applications.

Working with SQLite Databases on Mobile


Introduction to SQLite Databases
Today let's dive into SQLite databases on mobile devices. SQLite is a popular choice for local data
storage due to its lightweight nature and flexibility. It allows mobile applications to store structured
data efficiently and securely.

Setting Up SQLite Database


1. Database Creation
To work with SQLite databases, you first need to create a database. This involves defining the
database schema, including tables, columns, and relationships between them. You can use SQL
commands to create and modify the structure of the database.

2. Database Helper Class


Creating a helper class that extends SQLiteOpenHelper is a common practice. This class helps in
managing database creation, upgrades, and provides easy access to the database.

public class DBHelper extends SQLiteOpenHelper {


// Constructor and necessary methods here
}

Performing CRUD Operations


1. Inserting Data
To insert data into a SQLite database, you can use SQL INSERT statements or Android's built-in
methods like insert().
2. Querying Data
Queries can be performed using SELECT statements. You can fetch data based on specific criteria
and retrieve results as needed.

3. Updating Data
Updating existing data in the database can be done using SQL UPDATE statements or methods
like update() provided by Android.

4. Deleting Data
Deletion operations can be performed using SQL DELETE statements or methods like delete().

Using Room Persistence Library (Optional)


What is Room?
Room is a SQLite object mapping library provided by Android Jetpack. It simplifies working with
SQLite databases by providing compile-time verification of SQL queries and reducing boilerplate
code.

Key Components of Room


- Entity: Represents a table within the database.
- DAO (Data Access Object): Contains methods for accessing the database.
- Database: Acts as the main access point for the underlying SQLite database.

Best Practices for Working with SQLite


- Use transactions for bulk insertions or updates to maintain data integrity.
- Implement proper error handling to manage database exceptions.
- Use indexes for faster data retrieval on large datasets.
- Consider asynchronous operations to prevent UI blocking during database operations.

Conclusion
SQLite databases are a powerful tool for storing and managing structured data on mobile devices.
By understanding the basics of SQLite, setting up databases, performing CRUD operations, and
utilizing libraries like Room, developers can create efficient and robust database-driven mobile
applications.
Using SharedPreferences for Data Persistence
Introduction to SharedPreferences
Let's talk about SharedPreferences—a simple and lightweight way to store key-value pairs of
primitive data in Android. SharedPreferences are commonly used for storing small amounts of
data persistently, such as user settings, preferences, and app configurations.

Setting Up SharedPreferences
1. Obtaining SharedPreferences Instance
To work with SharedPreferences, you need to obtain an instance of SharedPreferences associated
with your application's package using the Context:
SharedPreferences sharedPreferences = context.getSharedPreferences("my_preferences",
Context.MODE_PRIVATE);

2. Editor for Making Changes


To modify the SharedPreferences data, you need to obtain an instance of
SharedPreferences.Editor:

SharedPreferences.Editor editor = sharedPreferences.edit();

Storing Data
1. Storing Data in SharedPreferences
You can store data by using the appropriate methods in SharedPreferences.Editor, such as
putString(), putInt(), putBoolean(), etc. For example:

editor.putString("username", "example_user");
editor.putInt("age", 25);
editor.putBoolean("is_logged_in", true);
editor.apply(); // Commit the changes

Retrieving Data
1. Retrieving Data from SharedPreferences
To retrieve data from SharedPreferences, simply use the getter methods provided by
SharedPreferences. For instance:
String username = sharedPreferences.getString("username", "default_value");
int age = sharedPreferences.getInt("age", 0);
boolean isLoggedIn = sharedPreferences.getBoolean("is_logged_in", false);

Best Practices for Using SharedPreferences


- Encrypt Sensitive Data: If you need to store sensitive data, consider encrypting it before storing
it in SharedPreferences to ensure data security.
- Limit Data Size: SharedPreferences are suitable for small amounts of data. For larger datasets,
consider using other storage methods like SQLite.
- Avoid Complex Data Structures: Stick to simple key-value pairs in SharedPreferences. For
complex data structures, consider using JSON serialization/deserialization.

Conclusion
SharedPreferences provide a convenient way to store and retrieve simple data persistently in
Android applications. By using SharedPreferences effectively, developers can easily manage user
preferences, settings, and small data sets without the complexity of a full-fledged database.

Practical Hands-on Exercises on Implementing Data Management in Mobile Apps


CONTINUOUS ASSESSMENT (CA)
1. Store a User's Name in SharedPreferences
- Task: Implement a feature to store and retrieve a user's name using SharedPreferences.
2. Create an SQLite Database Table for User Information
- Task: Define a SQLite database table to store user information like name, email, and age.
3. Insert Data into SQLite Database
- Task: Insert a new user record into the SQLite database.
4. Fetch Data from SQLite Database
- Task: Retrieve the list of all users from the SQLite database.
5. Update User's Age in SQLite Database
- Task: Update the age of a specific user in the SQLite database.
6. Implement Data Persistence Using Room Library
- Task: Define an entity and DAO interface for a simple data model using Room library.
7. Store User's Settings Using SharedPreferences
- Task: Save and retrieve user settings (like theme preference) using SharedPreferences.
8. Implement Data Encryption for SharedPreferences
- Task: Encrypt sensitive data (e.g., password) before storing it in SharedPreferences for added
security.
9. Handle Data Syncing between Local Database and Server
- Task: Implement synchronization logic to keep local data consistent with server data in a
mobile app.
10. Optimize Data Access with Content Providers
- Task: Implement a Content Provider to enable efficient data sharing and access between
different app components.

You might also like