SlideShare une entreprise Scribd logo
1  sur  43
Télécharger pour lire hors ligne
5
 Saving Data
 Anuchit Chalothorn
 anoochit@gmail.com

Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Data Storage
Android provides several options for you to
save persistent application data.
● Share Preference
● Internal storage
● External Storage
● SQLite Database
● Network Connection
Shared Preference
The SharedPreferences class provides a
general framework that allows you to save and
retrieve persistent key-value pairs of primitive
data types.
Android App Development 05 : Saving Data
Setting Activity
Applications often include settings that allow
users to modify app features and behaviors.
You should use Android's Preference APIs to
build an interface that's consistent with the user
experience in other Android apps (including the
system settings).




Ref: http://developer.android.com/guide/topics/ui/settings.html
Workshop: Setting Activity
Create Setting Activity to store username and
password in shared preference data
Android App Development 05 : Saving Data
Internal Storage
Access to the file system is performed via the
standard java.io classes. Android provides also
helper classes for creating and accessing new
files and directories.
Internal Storage
For example the getDir(String, int) method
would create or access a directory. The
openFileInput(String s) method would open a
file for input and openFileOutput(String s, int)
would create a file.
Permission Mode
int specifies the permissions which are:
● MODE_PRIVATE
  ○ No access for other applications
● MODE_WORLD_READABLE
  ○ Read access for other applications
● MODE_WORLD_WRITABLE
  ○ Write access for other applications
● MODE_WORLD_READABLE | MODE_WORLD_WRITABLE
  ○ Read / Write access
Environment Variable
●   DIRECTORY_ALARMS
●   DIRECTORY_DCIM
●   DIRECTORY_DOWNLOADS
●   DIRECTORY_MOVIES
●   DIRECTORY_MUSIC
●   DIRECTORY_NOTIFICATIONS
●   DIRECTORY_PICTURES
●   DIRECTORY_PODCASTS
●   DIRECTORY_RINGTONES
Workshop: Get your path
Android provide methods to get path from
external storage and internal storage

File internal_path,external_path;
internal_path=getDir(Environment.DIRECTORY_DOWNLOADS, 0);
external_path= Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS);
Android App Development 05 : Saving Data
SQLite
SQLite is an Open Source Database which is
embedded into Android. SQLite supports
standard relational database features like SQL
syntax, transactions and prepared statements.
Useful Tools
● SQLite database browser
● SQLite Manager
SQLite in Android
SQLite is available on every Android device,
does not require any database setup or
administration. You only have to define the
SQL statements for creating and updating the
database.
SQLite Open Helper
To create and upgrade a database in your
Android application you usually subclass
SQLiteOpenHelper. In the constructor of your
subclass you call the super() method of
SQLiteOpenHelper, specifying the database
name and the current database version.
Workshop: SQLite Open Helper
Create blank app with database helper class
extend from SQLiteOpenHelper class, identify
database name, version number and create
table at onCreate method. Call database helper
from main activity. Your database will create at
/data/data/packagename/files/
databasename
Android App Development 05 : Saving Data
Workshop: SQLite External Storage
Create blank app with database helper class
extend from SQLiteOpenHelper class, identify
database name which store in external storage
files directory, version number and create table
at onCreate method. Call database helper from
main activity. Your database will create at
/sdcard/Android/data/packagename/
files/databasename.db
Android App Development 05 : Saving Data
Workshop: CRUD
Create an app to store, select, update and
delete. Using following scenarios:
● Insert 2 rows
    ○ 1, Jim, 023645456
    ○ 2, John, 023645876
●   Select John
●   Select all records
●   Update Jim's number to 024953658
●   Delete John
Android App Development 05 : Saving Data
Workshop: Insert Data
Use insert() method to insert data in SQLite
table.

ContentValues val = new ContentValues();
val.put("name", name);
val.put("tel", tel);
long row = db.insert("member", null, val);
Android App Development 05 : Saving Data
Query
Queries can be created via the rawQuery() and
query() methods or via the SQLiteQueryBuilder
class .
● rawQuery() directly accepts an SQL select
   statement as input.
● query() provides a structured interface for
   specifying the SQL query.
Query Example
Method rawQuery() Example

Cursor cursor = getReadableDatabase().
  rawQuery("select * from todo where _id =
?", new String[] {id});
Query Example
Method query() Example

database.query(DATABASE_TABLE,
  new String[] { KEY_ROWID, KEY_CATEGORY,
KEY_SUMMARY, KEY_DESCRIPTION },
  null, null, null, null, null);
Cursor
A query returns a Cursor object. A Cursor
represents the result of a query and basically
points to one row of the query result. This way
Android can buffer the query results efficiently;
as it does not have to load all data into
memory.
Cursor
To move between individual data rows, you can
use the moveToFirst() and moveToNext()
methods. The isAfterLast() method allows to
check if the end of the query result has been
reached.
Workshop: Select
Use db.query to query your data and return into
Array.
Android App Development 05 : Saving Data
Workshop: Select All
Two ways to return select all records depend
on return data; Array and ArrayList. Use db.
rawQuery to query your data.
Android App Development 05 : Saving Data
Workshop: Update
Use a db.update() method to update data in
specific record and criteria.
Android App Development 05 : Saving Data
Workshop: Delete
Use a db.delete() method to delete data in
specific record and criteria.
Android App Development 05 : Saving Data
Put it together in real life!
You may pre load data in the app or try to
update data from internet, so the easy way put
it in asset and copy to another storage.
Android App Development 05 : Saving Data
Push data to a ListView
Query data and return to an ArrayList then
push ArrayList to ListView.
Android App Development 05 : Saving Data
End

Contenu connexe

Tendances

Android datastorage
Android datastorageAndroid datastorage
Android datastorageKrazy Koder
 
[Android] Data Storage
[Android] Data Storage[Android] Data Storage
[Android] Data StorageNikmesoft Ltd
 
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursorsinfo_zybotech
 
Android Database Tutorial
Android Database TutorialAndroid Database Tutorial
Android Database TutorialPerfect APK
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)Khaled Anaqwa
 
Data Storage In Android
Data Storage In Android Data Storage In Android
Data Storage In Android Aakash Ugale
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate TutorialSyed Shahul
 
Android - Data Storage
Android - Data StorageAndroid - Data Storage
Android - Data StorageMingHo Chang
 
Android Training (Storing & Shared Preferences)
Android Training (Storing & Shared Preferences)Android Training (Storing & Shared Preferences)
Android Training (Storing & Shared Preferences)Khaled Anaqwa
 
Advanced Core Data
Advanced Core DataAdvanced Core Data
Advanced Core DataMake School
 
Data Management for Quantitative Biology - Database Systems (continued) LIMS ...
Data Management for Quantitative Biology - Database Systems (continued) LIMS ...Data Management for Quantitative Biology - Database Systems (continued) LIMS ...
Data Management for Quantitative Biology - Database Systems (continued) LIMS ...QBiC_Tue
 
Data Management for Quantitative Biology - Database systems, May 7, 2015, Dr....
Data Management for Quantitative Biology - Database systems, May 7, 2015, Dr....Data Management for Quantitative Biology - Database systems, May 7, 2015, Dr....
Data Management for Quantitative Biology - Database systems, May 7, 2015, Dr....QBiC_Tue
 
Database Programming
Database ProgrammingDatabase Programming
Database ProgrammingHenry Osborne
 
Shared preferences
Shared preferencesShared preferences
Shared preferencesSourabh Sahu
 

Tendances (20)

Android datastorage
Android datastorageAndroid datastorage
Android datastorage
 
[Android] Data Storage
[Android] Data Storage[Android] Data Storage
[Android] Data Storage
 
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursors
 
Android Database Tutorial
Android Database TutorialAndroid Database Tutorial
Android Database Tutorial
 
Android-data storage in android-chapter21
Android-data storage in android-chapter21Android-data storage in android-chapter21
Android-data storage in android-chapter21
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
Data Storage In Android
Data Storage In Android Data Storage In Android
Data Storage In Android
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
 
Android Data Storagefinal
Android Data StoragefinalAndroid Data Storagefinal
Android Data Storagefinal
 
Android - Data Storage
Android - Data StorageAndroid - Data Storage
Android - Data Storage
 
บทที่4
บทที่4บทที่4
บทที่4
 
9 content-providers
9 content-providers9 content-providers
9 content-providers
 
Weka
WekaWeka
Weka
 
Android Training (Storing & Shared Preferences)
Android Training (Storing & Shared Preferences)Android Training (Storing & Shared Preferences)
Android Training (Storing & Shared Preferences)
 
Advanced Core Data
Advanced Core DataAdvanced Core Data
Advanced Core Data
 
Storage 8
Storage   8Storage   8
Storage 8
 
Data Management for Quantitative Biology - Database Systems (continued) LIMS ...
Data Management for Quantitative Biology - Database Systems (continued) LIMS ...Data Management for Quantitative Biology - Database Systems (continued) LIMS ...
Data Management for Quantitative Biology - Database Systems (continued) LIMS ...
 
Data Management for Quantitative Biology - Database systems, May 7, 2015, Dr....
Data Management for Quantitative Biology - Database systems, May 7, 2015, Dr....Data Management for Quantitative Biology - Database systems, May 7, 2015, Dr....
Data Management for Quantitative Biology - Database systems, May 7, 2015, Dr....
 
Database Programming
Database ProgrammingDatabase Programming
Database Programming
 
Shared preferences
Shared preferencesShared preferences
Shared preferences
 

En vedette

En vedette (9)

Android App Development 01 : Getting Start
Android App Development 01 : Getting StartAndroid App Development 01 : Getting Start
Android App Development 01 : Getting Start
 
Drupal Workshop
Drupal WorkshopDrupal Workshop
Drupal Workshop
 
Introduction to Tiny Blogr
Introduction to Tiny BlogrIntroduction to Tiny Blogr
Introduction to Tiny Blogr
 
Introduction to RedLine Software
Introduction to RedLine SoftwareIntroduction to RedLine Software
Introduction to RedLine Software
 
Lanai CMS 2
Lanai CMS 2Lanai CMS 2
Lanai CMS 2
 
10+3 fonts
10+3 fonts10+3 fonts
10+3 fonts
 
What is open source
What is open sourceWhat is open source
What is open source
 
Android App Development 09 : Publish & Monetize
Android App Development 09 : Publish & MonetizeAndroid App Development 09 : Publish & Monetize
Android App Development 09 : Publish & Monetize
 
Open Source Operating System for Education (supervisors)
Open Source Operating System for Education (supervisors)Open Source Operating System for Education (supervisors)
Open Source Operating System for Education (supervisors)
 

Similaire à Android App Development 05 : Saving Data

Android SQLite Database And Content Provider Tutorial
Android SQLite Database And Content Provider TutorialAndroid SQLite Database And Content Provider Tutorial
Android SQLite Database And Content Provider TutorialJeff Brooks
 
Create an android app for database creation using.pptx
Create an android app for database creation using.pptxCreate an android app for database creation using.pptx
Create an android app for database creation using.pptxvishal choudhary
 
Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)Khaled Anaqwa
 
12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptx12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptxFaezNasir
 
Tutorial mvc (pelajari ini jika ingin tahu mvc) keren
Tutorial mvc (pelajari ini jika ingin tahu mvc) kerenTutorial mvc (pelajari ini jika ingin tahu mvc) keren
Tutorial mvc (pelajari ini jika ingin tahu mvc) kerenSony Suci
 
Data Handning with Sqlite for Android
Data Handning with Sqlite for AndroidData Handning with Sqlite for Android
Data Handning with Sqlite for AndroidJakir Hossain
 
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...TAISEEREISA
 
Local storage in Web apps
Local storage in Web appsLocal storage in Web apps
Local storage in Web appsIvano Malavolta
 
Sqlite
SqliteSqlite
SqliteKumar
 
Microsoft Entity Framework
Microsoft Entity FrameworkMicrosoft Entity Framework
Microsoft Entity FrameworkMahmoud Tolba
 
Asp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptxAsp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptxsridharu1981
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.pptDrMeenakshiS
 

Similaire à Android App Development 05 : Saving Data (20)

Android SQLite Database And Content Provider Tutorial
Android SQLite Database And Content Provider TutorialAndroid SQLite Database And Content Provider Tutorial
Android SQLite Database And Content Provider Tutorial
 
Android sq lite-chapter 22
Android sq lite-chapter 22Android sq lite-chapter 22
Android sq lite-chapter 22
 
Create an android app for database creation using.pptx
Create an android app for database creation using.pptxCreate an android app for database creation using.pptx
Create an android app for database creation using.pptx
 
Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)
 
12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptx12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptx
 
Tutorial mvc (pelajari ini jika ingin tahu mvc) keren
Tutorial mvc (pelajari ini jika ingin tahu mvc) kerenTutorial mvc (pelajari ini jika ingin tahu mvc) keren
Tutorial mvc (pelajari ini jika ingin tahu mvc) keren
 
Data Handning with Sqlite for Android
Data Handning with Sqlite for AndroidData Handning with Sqlite for Android
Data Handning with Sqlite for Android
 
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
 
Data access
Data accessData access
Data access
 
SQLite database in android
SQLite database in androidSQLite database in android
SQLite database in android
 
Jdbc
JdbcJdbc
Jdbc
 
Android sql examples
Android sql examplesAndroid sql examples
Android sql examples
 
Spring 3 to 4
Spring 3 to 4Spring 3 to 4
Spring 3 to 4
 
Local storage in Web apps
Local storage in Web appsLocal storage in Web apps
Local storage in Web apps
 
Sqlite
SqliteSqlite
Sqlite
 
Microsoft Entity Framework
Microsoft Entity FrameworkMicrosoft Entity Framework
Microsoft Entity Framework
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Asp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptxAsp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptx
 
Model viewviewmodel2
Model viewviewmodel2Model viewviewmodel2
Model viewviewmodel2
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
 

Plus de Anuchit Chalothorn (20)

Flutter Workshop 2021 @ ARU
Flutter Workshop 2021 @ ARUFlutter Workshop 2021 @ ARU
Flutter Workshop 2021 @ ARU
 
Flutter workshop @ bang saen 2020
Flutter workshop @ bang saen 2020Flutter workshop @ bang saen 2020
Flutter workshop @ bang saen 2020
 
13 web service integration
13 web service integration13 web service integration
13 web service integration
 
09 material design
09 material design09 material design
09 material design
 
07 intent
07 intent07 intent
07 intent
 
05 binding and action
05 binding and action05 binding and action
05 binding and action
 
04 layout design and basic widget
04 layout design and basic widget04 layout design and basic widget
04 layout design and basic widget
 
03 activity life cycle
03 activity life cycle03 activity life cycle
03 activity life cycle
 
02 create your first app
02 create your first app02 create your first app
02 create your first app
 
01 introduction
01 introduction 01 introduction
01 introduction
 
Material Theme
Material ThemeMaterial Theme
Material Theme
 
00 Android Wear Setup Emulator
00 Android Wear Setup Emulator00 Android Wear Setup Emulator
00 Android Wear Setup Emulator
 
MongoDB Replication Cluster
MongoDB Replication ClusterMongoDB Replication Cluster
MongoDB Replication Cluster
 
MongoDB Shard Cluster
MongoDB Shard ClusterMongoDB Shard Cluster
MongoDB Shard Cluster
 
IT Automation with Chef
IT Automation with ChefIT Automation with Chef
IT Automation with Chef
 
IT Automation with Puppet Enterprise
IT Automation with Puppet EnterpriseIT Automation with Puppet Enterprise
IT Automation with Puppet Enterprise
 
Using PhoneGap Command Line
Using PhoneGap Command LineUsing PhoneGap Command Line
Using PhoneGap Command Line
 
Collaborative development with Git | Workshop
Collaborative development with Git | WorkshopCollaborative development with Git | Workshop
Collaborative development with Git | Workshop
 
OpenStack Cheat Sheet V2
OpenStack Cheat Sheet V2OpenStack Cheat Sheet V2
OpenStack Cheat Sheet V2
 
REST API with CakePHP
REST API with CakePHPREST API with CakePHP
REST API with CakePHP
 

Android App Development 05 : Saving Data

  • 1. 5 Saving Data Anuchit Chalothorn anoochit@gmail.com Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  • 2. Data Storage Android provides several options for you to save persistent application data. ● Share Preference ● Internal storage ● External Storage ● SQLite Database ● Network Connection
  • 3. Shared Preference The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types.
  • 5. Setting Activity Applications often include settings that allow users to modify app features and behaviors. You should use Android's Preference APIs to build an interface that's consistent with the user experience in other Android apps (including the system settings). Ref: http://developer.android.com/guide/topics/ui/settings.html
  • 6. Workshop: Setting Activity Create Setting Activity to store username and password in shared preference data
  • 8. Internal Storage Access to the file system is performed via the standard java.io classes. Android provides also helper classes for creating and accessing new files and directories.
  • 9. Internal Storage For example the getDir(String, int) method would create or access a directory. The openFileInput(String s) method would open a file for input and openFileOutput(String s, int) would create a file.
  • 10. Permission Mode int specifies the permissions which are: ● MODE_PRIVATE ○ No access for other applications ● MODE_WORLD_READABLE ○ Read access for other applications ● MODE_WORLD_WRITABLE ○ Write access for other applications ● MODE_WORLD_READABLE | MODE_WORLD_WRITABLE ○ Read / Write access
  • 11. Environment Variable ● DIRECTORY_ALARMS ● DIRECTORY_DCIM ● DIRECTORY_DOWNLOADS ● DIRECTORY_MOVIES ● DIRECTORY_MUSIC ● DIRECTORY_NOTIFICATIONS ● DIRECTORY_PICTURES ● DIRECTORY_PODCASTS ● DIRECTORY_RINGTONES
  • 12. Workshop: Get your path Android provide methods to get path from external storage and internal storage File internal_path,external_path; internal_path=getDir(Environment.DIRECTORY_DOWNLOADS, 0); external_path= Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DOWNLOADS);
  • 14. SQLite SQLite is an Open Source Database which is embedded into Android. SQLite supports standard relational database features like SQL syntax, transactions and prepared statements.
  • 15. Useful Tools ● SQLite database browser ● SQLite Manager
  • 16. SQLite in Android SQLite is available on every Android device, does not require any database setup or administration. You only have to define the SQL statements for creating and updating the database.
  • 17. SQLite Open Helper To create and upgrade a database in your Android application you usually subclass SQLiteOpenHelper. In the constructor of your subclass you call the super() method of SQLiteOpenHelper, specifying the database name and the current database version.
  • 18. Workshop: SQLite Open Helper Create blank app with database helper class extend from SQLiteOpenHelper class, identify database name, version number and create table at onCreate method. Call database helper from main activity. Your database will create at /data/data/packagename/files/ databasename
  • 20. Workshop: SQLite External Storage Create blank app with database helper class extend from SQLiteOpenHelper class, identify database name which store in external storage files directory, version number and create table at onCreate method. Call database helper from main activity. Your database will create at /sdcard/Android/data/packagename/ files/databasename.db
  • 22. Workshop: CRUD Create an app to store, select, update and delete. Using following scenarios: ● Insert 2 rows ○ 1, Jim, 023645456 ○ 2, John, 023645876 ● Select John ● Select all records ● Update Jim's number to 024953658 ● Delete John
  • 24. Workshop: Insert Data Use insert() method to insert data in SQLite table. ContentValues val = new ContentValues(); val.put("name", name); val.put("tel", tel); long row = db.insert("member", null, val);
  • 26. Query Queries can be created via the rawQuery() and query() methods or via the SQLiteQueryBuilder class . ● rawQuery() directly accepts an SQL select statement as input. ● query() provides a structured interface for specifying the SQL query.
  • 27. Query Example Method rawQuery() Example Cursor cursor = getReadableDatabase(). rawQuery("select * from todo where _id = ?", new String[] {id});
  • 28. Query Example Method query() Example database.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_CATEGORY, KEY_SUMMARY, KEY_DESCRIPTION }, null, null, null, null, null);
  • 29. Cursor A query returns a Cursor object. A Cursor represents the result of a query and basically points to one row of the query result. This way Android can buffer the query results efficiently; as it does not have to load all data into memory.
  • 30. Cursor To move between individual data rows, you can use the moveToFirst() and moveToNext() methods. The isAfterLast() method allows to check if the end of the query result has been reached.
  • 31. Workshop: Select Use db.query to query your data and return into Array.
  • 33. Workshop: Select All Two ways to return select all records depend on return data; Array and ArrayList. Use db. rawQuery to query your data.
  • 35. Workshop: Update Use a db.update() method to update data in specific record and criteria.
  • 37. Workshop: Delete Use a db.delete() method to delete data in specific record and criteria.
  • 39. Put it together in real life! You may pre load data in the app or try to update data from internet, so the easy way put it in asset and copy to another storage.
  • 41. Push data to a ListView Query data and return to an ArrayList then push ArrayList to ListView.
  • 43. End