Mobile Development Dr Msury Mahunnah email msury mahunnahifm
Mobile Development Dr. Msury Mahunnah e-mail: msury. mahunnah@ifm. ac. tz course website: https: //mahunnah. wordpress. com/
Lecture 10 Data and Storage
Outline • • • Introduction Data storage options Shared Preferences Internal Storage External Storage SQLite Database
Introduction • Android provides several options to save persistent application data • Choice depends on specific needs i. e. if data should be private or not, how much space the data requires, what type of data it is • Options are: – – Shared Preferences Internal Storage External Storage SQLite Databases
Shared Preferences • Stores private primitive data in key-value pairs • The Preference. Manager class provides methods to get access to preferences stored in a certain file • Preference value are accessed via the key and the instance of the Shared. Preferences class
Shared Preferences • To create or change preferences you have to call the edit() method on the Shared. Preferences object • After the change you call apply() to apply your changes asynchronously • You can also listen for changes:
Internal Storage • Store private data on the device’s internal memory • When user uninstalls app data is removed • To create and write a private file to the internal storage: – Call open. File. Output() with the name of the file and the operating mode. This returns a File. Output. Stream – Write to the file with write() – Close the stream with close()
External Storage • Every Android-compatible device supports a shared "external storage" that you can use to save files • Removable storage media (such as an SD card) or an internal (non-removable) storage • World-readable – any application can read
Using External Storage • Get READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permissions • Check media availability before writing or reading
SQLite database • Open source database • Supports SQL syntax, transactions & prepared statements, data types such as TEXT, INTEGER & REAL (even though does not validate) • Embedded into every Android device • SQLite DB access is slow (involves file system). Therefore, asynchronous access is recommended. • DATA/data/APP_NAME/databases/FILENAME • android. database. *
Using SQLite database • Create subclass of the SQLite. Open. Helper class • Override the following methods to create and update your database – on. Create() – called, if the database is accessed but not yet created – on. Upgrade() - called, if the database version is increased in your application code • The SQLite. Open. Helper class provides the get. Readable. Database() and get. Writeable. Database() methods to get access to an SQLite. Database object • Database tables should use _id for primary key
Querying SQLite database is the base class which provides methods to open, query, update and close the database • i. e. insert(), update() and delete() • Also exec. SQL() method, which allows to execute an SQL statement directly • Content. Values class allows to define key/values used for inserts and updates • SQLite. Database – Key – table column – Value – content of the record in this column • Queries can be created via the raw. Query() and query() methods or via the SQLite. Query. Builder class
Querying SQLite database • A query returns a Cursor object • Represents the result of a query and basically points to one row of the query result • Get the number of elements of the resulting query get. Count() • Move between individual data rows - move. To. First(), move. To. Next() and is. After. Last() methods to access the column data for the current position of the result e. g. get. Long(column. Index), get. String(column. Index) • Closed with close() method • get*()
Loaders • Next Lecturer
- Slides: 18