SlideShare a Scribd company logo
Data Storage
Lecture # 11
Software for Mobile Devices
Shared Preferences
• Shared Preferences are XML files to store private primitive
data in key-value pairs. Data Types include Booleans, floats,
ints, longs, and strings.
• Data saved in SharedPreferences file is accessible throughout
the application and persists even after the application closes
or across reboots.
• In order to use shared preferences, you have to call
a method getSharedPreferences() that returns a
SharedPreference instance pointing to the file that
contains the values of preferences.
How to use?
SharedPreferences sharedpreferences = getSharedPreferences
(“AppPreferences”, Context.MODE_PRIVATE);
• The first parameter is the key and the second
parameter is the MODE.
The data saved using SharedPreferences will be available
even after your application is killed.
The data persists when your application is upgraded!!
• You can save something in the sharedpreferences
by using SharedPreferences.Editor class
How to write something in shared
preference?
sharedpreferences.edit().putString("key", "value").apply();
SharedPreferences sharedpreferences =
getSharedPreferences(“AppPreferences”, Context.MODE_PRIVATE);
sharedpreferences.edit().putString("key", "value").commit();
Commit() saves data synchronously where as
apply() saves data in background (asynchronous).
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.putString(Phone, ph);
editor.putString(Email, e);
editor.apply();
OR
Shared Preference Example
1. SharedPreferences sharedpreferences = getSharedPreferences
(“AppPreferences”, Context.MODE_PRIVATE);
1. String value = sharedpreferences.getString("key", "");
2. String value = sharedpreferences.getInt("key", 0);
Second Param is the Value to return if this preference does not exist.
Reading from Shared Preference
Using Sqlite for local data storage
SQLite is a opensource SQL database that stores data to a text file on a
device. Android comes in with built in SQLite database implementation.
SQLite supports all the relational database features. In order to access this
database, you don't need to establish any kind of connections for it like
JDBC,ODBC e.t.c
The main package is android.database.sqlite that contains the classes
to manage your own databases
Database - Helper class
For managing all the operations related to the database , an helper class
has been given and is called SQLiteOpenHelper. It automatically
manages the creation and update of the database.
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(){
super(context,DATABASE_NAME,null,1);
}
public void onCreate(SQLiteDatabase db) {
}
public void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {
}
}
Desktop sql viewer: https://sqlitebrowser.org/
https://stackoverflow.com/questions/17529766/view-
contents-of-database-file-in-android-studio
Creating a Database & Table
Inserting a Row in Table
1. Inserting data requires getting writable
instance (getWriteableDatabase()) on
database.
1. ContentValues() is used to define the
column name and its data to be stored.
Here, we are just setting the note value only
ignoring `id` and `timestamp` as these two
will be inserted automatically.
1. Every time the database connection has to
be closed once you are done with
database access. Calling db.close() closes
the connection.
Reading a Row in Table
1. Reading data requires getting readable
instance (getReadableDatabase()) on
database.
1. Db.query with params table and column
names with their values return the data set
to cursor.
1. Every time the database connection has to
be closed once you are done with
database access. Calling db.close() closes
the connection.
Cursors are what contain the result set of a query made
against a database in Android
db.close();
Database Libraries
1. Room
The Room persistence library provides an abstraction layer over SQLite to allow
for more robust database access while harnessing the full power of SQLite.
https://developer.android.com/training/data-storage/room/
2. Ormlite
ORMLite is lighter version of Object Relational Mapping which provide some
simple functionality for persisting java objects to SQL databases. It is ORM
wrapper over any mobile SQL related DB.
http://ormlite.com/android/examples/
http://ormlite.com/
1. GreenDao
greenDAO is an open source Android ORM making development for SQLite
databases fun again. It relieves developers from dealing with low-level
database requirements while saving development time.
http://greenrobot.org/greendao/
Tutorial: https://datarockets.com/blog/android-architecture-migrate-to-room-from-ormlite/
https://www.tldevtech.com/best-android-orm-libraries-to-use/
Permissions
Request permissions at runtime appropriately (in Android 6.0+)
1. Make sure you request permissions at the right time and appropriately educate
and inform your users about the need for permission requests.
Best practices
1. Educate the user before requesting a permission
2. Always check to see if the permission has been granted
3. Ensure users benefit from allowing a permission immediately
4. Help users undo permission denials
Tutorials: https://www.youtube.com/watch?v=iZqDdvhTZj0
https://www.youtube.com/watch?v=C8lUdPVSzDk
https://developer.android.com/training/permissions/requesting
Permissions
You should always check for and request permissions at runtime to guard against
runtime errors (SecurityException).
Dangerous Permissions
Dangerous permissions cover areas where the
app wants data or resources that involve the
user's private information, or could potentially
affect the user's stored data or the operation of
other apps
Only dangerous permissions require user
agreement.
An app must publicize the permissions it requires by
including <uses-permission> tags in the app manifest.
Requesting Permissions
Tutorials: https://www.youtube.com/watch?v=iZqDdvhTZj0
https://www.youtube.com/watch?v=C8lUdPVSzDk
https://developer.android.com/training/permissions/requesting
Homework
Create your semester project in Android Studio, integrate any database
library (Room, ormlite etc) for your project and write a simple
read/write/update operation.

More Related Content

DOCX
Android-data storage in android-chapter21
PPT
Data Storage In Android
PDF
Mobile Application Development -Lecture 11 & 12.pdf
PPTX
Mobile application Development-UNIT-V (1).pptx
PPTX
Create an android app for database creation using.pptx
PPTX
Data Handning with Sqlite for Android
PDF
Android App Development 05 : Saving Data
PDF
Mobile Application Development-Lecture 13 & 14.pdf
Android-data storage in android-chapter21
Data Storage In Android
Mobile Application Development -Lecture 11 & 12.pdf
Mobile application Development-UNIT-V (1).pptx
Create an android app for database creation using.pptx
Data Handning with Sqlite for Android
Android App Development 05 : Saving Data
Mobile Application Development-Lecture 13 & 14.pdf

Similar to 12_Data_Storage_Part_2.pptx (20)

PPTX
iOS Course day 2
PDF
Persistence on iOS
PDF
Android datastorage
PDF
Implementation of sql server based on sqlite engine on
PPTX
PHP Oracle
PPTX
Microsoft Entity Framework
PPT
Sqllite
PDF
Database Performance Management in Cloud
DOCX
Entity Framework
PPTX
PPT temp.pptx
DOCX
Database management system
PPTX
Spring data jpa are used to develop spring applications
PDF
How do I - Storage, FileSystem & SQL - Transcript.pdf
PDF
Android - Saving data
PPTX
Fundamentals of JAVA
PDF
Android Architecture Components
PDF
DSC - Shared Preferences and Room
PPTX
Ef code first
PPT
High Availability And Oracle Data Guard 11g R2
PPTX
Database in Android
iOS Course day 2
Persistence on iOS
Android datastorage
Implementation of sql server based on sqlite engine on
PHP Oracle
Microsoft Entity Framework
Sqllite
Database Performance Management in Cloud
Entity Framework
PPT temp.pptx
Database management system
Spring data jpa are used to develop spring applications
How do I - Storage, FileSystem & SQL - Transcript.pdf
Android - Saving data
Fundamentals of JAVA
Android Architecture Components
DSC - Shared Preferences and Room
Ef code first
High Availability And Oracle Data Guard 11g R2
Database in Android

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Business Ethics Teaching Materials for college
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
01-Introduction-to-Information-Management.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Business Ethics Teaching Materials for college
Microbial disease of the cardiovascular and lymphatic systems
O7-L3 Supply Chain Operations - ICLT Program
Pharma ospi slides which help in ospi learning
Renaissance Architecture: A Journey from Faith to Humanism
102 student loan defaulters named and shamed – Is someone you know on the list?
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
01-Introduction-to-Information-Management.pdf
Pre independence Education in Inndia.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
O5-L3 Freight Transport Ops (International) V1.pdf
Microbial diseases, their pathogenesis and prophylaxis
Basic Mud Logging Guide for educational purpose
Week 4 Term 3 Study Techniques revisited.pptx

12_Data_Storage_Part_2.pptx

  • 1. Data Storage Lecture # 11 Software for Mobile Devices
  • 2. Shared Preferences • Shared Preferences are XML files to store private primitive data in key-value pairs. Data Types include Booleans, floats, ints, longs, and strings. • Data saved in SharedPreferences file is accessible throughout the application and persists even after the application closes or across reboots.
  • 3. • In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences. How to use? SharedPreferences sharedpreferences = getSharedPreferences (“AppPreferences”, Context.MODE_PRIVATE); • The first parameter is the key and the second parameter is the MODE. The data saved using SharedPreferences will be available even after your application is killed. The data persists when your application is upgraded!!
  • 4. • You can save something in the sharedpreferences by using SharedPreferences.Editor class How to write something in shared preference? sharedpreferences.edit().putString("key", "value").apply(); SharedPreferences sharedpreferences = getSharedPreferences(“AppPreferences”, Context.MODE_PRIVATE); sharedpreferences.edit().putString("key", "value").commit(); Commit() saves data synchronously where as apply() saves data in background (asynchronous). SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Name, n); editor.putString(Phone, ph); editor.putString(Email, e); editor.apply(); OR
  • 5. Shared Preference Example 1. SharedPreferences sharedpreferences = getSharedPreferences (“AppPreferences”, Context.MODE_PRIVATE); 1. String value = sharedpreferences.getString("key", ""); 2. String value = sharedpreferences.getInt("key", 0); Second Param is the Value to return if this preference does not exist. Reading from Shared Preference
  • 6. Using Sqlite for local data storage SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation. SQLite supports all the relational database features. In order to access this database, you don't need to establish any kind of connections for it like JDBC,ODBC e.t.c The main package is android.database.sqlite that contains the classes to manage your own databases Database - Helper class For managing all the operations related to the database , an helper class has been given and is called SQLiteOpenHelper. It automatically manages the creation and update of the database. public class DBHelper extends SQLiteOpenHelper { public DBHelper(){ super(context,DATABASE_NAME,null,1); } public void onCreate(SQLiteDatabase db) { } public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { } } Desktop sql viewer: https://sqlitebrowser.org/ https://stackoverflow.com/questions/17529766/view- contents-of-database-file-in-android-studio
  • 8. Inserting a Row in Table 1. Inserting data requires getting writable instance (getWriteableDatabase()) on database. 1. ContentValues() is used to define the column name and its data to be stored. Here, we are just setting the note value only ignoring `id` and `timestamp` as these two will be inserted automatically. 1. Every time the database connection has to be closed once you are done with database access. Calling db.close() closes the connection.
  • 9. Reading a Row in Table 1. Reading data requires getting readable instance (getReadableDatabase()) on database. 1. Db.query with params table and column names with their values return the data set to cursor. 1. Every time the database connection has to be closed once you are done with database access. Calling db.close() closes the connection. Cursors are what contain the result set of a query made against a database in Android db.close();
  • 10. Database Libraries 1. Room The Room persistence library provides an abstraction layer over SQLite to allow for more robust database access while harnessing the full power of SQLite. https://developer.android.com/training/data-storage/room/ 2. Ormlite ORMLite is lighter version of Object Relational Mapping which provide some simple functionality for persisting java objects to SQL databases. It is ORM wrapper over any mobile SQL related DB. http://ormlite.com/android/examples/ http://ormlite.com/ 1. GreenDao greenDAO is an open source Android ORM making development for SQLite databases fun again. It relieves developers from dealing with low-level database requirements while saving development time. http://greenrobot.org/greendao/ Tutorial: https://datarockets.com/blog/android-architecture-migrate-to-room-from-ormlite/ https://www.tldevtech.com/best-android-orm-libraries-to-use/
  • 11. Permissions Request permissions at runtime appropriately (in Android 6.0+) 1. Make sure you request permissions at the right time and appropriately educate and inform your users about the need for permission requests. Best practices 1. Educate the user before requesting a permission 2. Always check to see if the permission has been granted 3. Ensure users benefit from allowing a permission immediately 4. Help users undo permission denials Tutorials: https://www.youtube.com/watch?v=iZqDdvhTZj0 https://www.youtube.com/watch?v=C8lUdPVSzDk https://developer.android.com/training/permissions/requesting
  • 12. Permissions You should always check for and request permissions at runtime to guard against runtime errors (SecurityException).
  • 13. Dangerous Permissions Dangerous permissions cover areas where the app wants data or resources that involve the user's private information, or could potentially affect the user's stored data or the operation of other apps Only dangerous permissions require user agreement. An app must publicize the permissions it requires by including <uses-permission> tags in the app manifest.
  • 15. Homework Create your semester project in Android Studio, integrate any database library (Room, ormlite etc) for your project and write a simple read/write/update operation.