You are on page 1of 23

Chapter 4

Data Storage and Retrieve


Android data storage

Android provides several options for you to save persistent application
data.

Storing the data persistently-Introducing the Data Storage Options:

Preferences,

Internal Storage,

External Storage,

SQLite database

Firebase ,

Android Preferences


Android shared preference is used to store and retrieve
primitive information. In android,

string, integer, long, number etc. are considered as primitive
data type.

Android Shared preferences are used to store data in key and
value pair so that we can retrieve the value on the basis of
key.
How can access them

To get a shared preferences object for your
application, use two methods

getSharedprefernces(int mode)

getpreferences(String name,int mode)
Modes

Modes refer to operating modes

Use 0 or MODE_PRIVATE for the default operation

MODE_PRIVATE :only your app can access the file.

MODE_WORLD_READABLE:All apps can read the file.

MODE_WORLD_WRITEABLE:All apps can write the
file.
Uses

Check whether user is using your app

Check when your app was last updated

Remember user setting

Remember user Credential

Location caching
How to use shared preferences to
store data

1.Get a references to a shared preference object

For a single file =>getpreferences(int mode)

For a several file=>getsharedPreferences(String name,int mode)

2. Call the editor

SharedPreferences.Editor ed =sharedpreferences.edit();

3.use the edtior to add the data with the key

ed.putString(“key”,”value”);

ed.puString(“key”,”value”);

4 Commit Editor changes

ed.commit(); OR ed.apply();

How to use shared preferences to
retrieve data

1. Get a reference to a shared preferences object

getPreferences(int mode)

getSharedPreferences(String name ,int mode)

2.Use the key provided earlier to get data

3.Supply the default value if the data is not found

String name=sharedPreferences,getString(“name”,”N/A”);

String password=sharedPreferences.getString(“password,”N/A”);
Internal storage

In android we can even save fils directly on the
devices internal storage

By default, files saved to the internal storage
are application cannot access them.

When the user uninstalls your application.
These files are removed.
Con’t

What kind of data can be stored in internal
storage

The type of data involves things such as a
text,file, image video file, audio file etc
External Storage

Like internal storage , we are able to save or read
data from the device external memory such as
sdcard.

Files saved to the external storage are world
readable and can be modified by the user when
they enable USB mass storage to transfer files on
a computer.

Android - SQLite Database

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



What is SQLite?

SQLite is an in-process library that implements a self-contained,
server less, zero-configuration, transactional SQL database
engine. It is the one database, which is zero-configured, that
means like other database you do not need to configure it in your
system.

SQLite engine is not a standalone process like other databases,
you can link it statically or

dynamically as per your requirement with your application. The
SQLite accesses its storage files directly.
Why SQLite?

SQLite does not require a separate server process or system to operate.
(serverless).

SQLite comes with zero-configuration, which means no setup or
administration

needed.

A complete SQLite database is stored in a single cross-platform disk file.

SQLite is very small and light weight, less than 400KiB fully configured or
less than 250KiB with optional features omitted.

SQLite is self-contained, which means no external dependencies.
Con’t

SQLite transactions are fully ACID-compliant, allowing safe access from multiple

processes or threads.

SQLite supports most of the query language features found in the SQL92 (SQL2)

standard.

SQLite is written in ANSI-C and provides simple and easy-to-use API.

SQLite is available on UNIX (Linux, Mac OS-X, Android, iOS) and Windows
(Win32,

WinCE, WinRT).
SQLite Limitation

There are few unsupported features of SQL92 in SQLite which are shown below:

Feature Description

RIGHT=======================>Only LEFT OUTER JOIN is implemented.

OUTER

JOIN

FULL==========================>Only LEFT OUTER JOIN is implemented.

OUTER

JOIN

ALTER======================>The RENAME TABLE and ADD COLUMN variants of the

TABLEALTER=================> TABLE command are supported. The DROP

COLUMN, ALTER COLUMN, ADD CONSTRAINT not supported
Con’t

TriggerFOR EACH ROW triggers are supported but not FOR

supportEACH STATEMENT triggers.

VIEWsVIEWs in SQLite are read-only. You may not execute a

DELETE, INSERT, or UPDATE statement on a view.

GRANT and The only access permissions that can be applied are the

REVOKE

normal file access permissions of the underlying

operating system.

SQLite Commands:

The standard SQLite commands to interact with
relational databases are similar as SQL.

They are CREATE, SELECT, INSERT, UPDATE,
DELETE and DROP. These commands can be

classified into groups based on their operational nature:

DDL - Data Definition Language:

Command and Description


CREATECreates a new table, a view of a table, or other
object in database

ALTERModifies an existing database object, such as a table.

DROPDeletes an entire table, a view of a table or other
object in the

database.
Con’t

DML - Data Manipulation Language:

INSERT=====>Creates a record

UPDATE=====>Modifies records

DELETE=====>Deletes records

DQL - Data Query Language:

SELECT Retrieves certain records from one or more tables
Firebase
Source Code


Refer your materials

Thank You

You might also like