Lecture 7 To 8 For Programming of Mobile Terminals
Lecture 7 To 8 For Programming of Mobile Terminals
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.
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.
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().
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);
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);
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.