Download LECTURE 5

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

PL/SQL wikipedia , lookup

SQL wikipedia , lookup

Data center wikipedia , lookup

Database wikipedia , lookup

Versant Object Database wikipedia , lookup

Data model wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Data analysis wikipedia , lookup

Forecasting wikipedia , lookup

Clusterpoint wikipedia , lookup

Information privacy law wikipedia , lookup

3D optical data storage wikipedia , lookup

Data vault modeling wikipedia , lookup

Relational model wikipedia , lookup

Business intelligence wikipedia , lookup

Database model wikipedia , lookup

Transcript
By: Eliav Menachi





On Android, all application data (including files) are
private to that application
Android provides a standard way for an application
to expose its private data to other applications content providers
Content provider exposes read/write access to the
application's data
Content providers implement a standard syntax for
requesting and modifying data
Content providers implement a standard
mechanism for reading the returned data





Preferences
Files
Database
Network
Content Providers
Preferences is a lightweight mechanism to store
and retrieve key-value pairs of primitive data
types
 Typically used to store application preferences
 Preferences cannot be shared across applications
(except by using a content provider)

Files can be stored directly on the mobile device or
on a removable storage medium
 By default, other applications cannot access these
files
 To read data from a file: Context.openFileInput
which returns a standard Java FileInputStream
object
 To write to a file: Context.openFileOutput which
returns a FileOutputStream object






Android supports creating and using SQLite databases
Each database is private to the application that creates it
The SQLiteDatabase object represents a database and has
methods for interacting with it
To create the database: SQLiteDatabase.create
Android ships with the sqlite3 database tool:
 enables to browse table contents
 run SQL commands
 perform other useful functions on SQLite databases

All databases, SQLite and others, are stored on the device in
/data/data/package_name/databases
Store and retrieve data
Make data accessible to all applications
there's no common storage area that all Android
packages can access
 Android ships with a number of content providers
for common data types:



 Audio
 Video
 Images
 Personal contact information
 and more (listed in android.provider package)
There are two options to make data public:
 Create a content provider
 Add the data to an existing content provider
 if there's one that controls the same type of data
 If you have permission to write to it




All content providers implement a common
interface for querying the provider and returning
results, adding, altering, and deleting data.
It's an interface that clients use indirectly
most generally through ContentResolver objects
You get a ContentResolver by calling
getContentResolver



Content providers expose their data as a
simple table on a database model
Each row is a record and each column is data
of a particular type and meaning
_ID field uniquely identifies the record within
the table

Each content provider exposes a public URI

URI uniquely identifies a data set

For multiple tables exposes a separate URI for
each table
Android defines CONTENT_URI constants for
all the providers that come with the platform

 android.provider.Contacts.Phones.CONTENT_URI
 android.provider.Contacts.Photos.CONTENT_URI
 A: "content://” - identifies the data as being controlled
by a content provider
 B: “com…/” - An authority - unique identifier used to
locate the provider in the provider registry
 C: “path section of the URI that is specific to each
provider
 D: ID of the row to fetch

Three pieces of information required to query
a content provider :
 The URI that identifies the provider
 The names of the data fields to receive
 The data types for those fields

There are two ways to query content
provider:
 ContentResolver.query
 Activity.managedQuery



Both take the same set of arguments
Both return a Cursor object
managedQuery causes the activity to
manage the life cycle of the Cursor
public final Cursor managedQuery (Uri uri, String[]
projection, String selection, String[]
selectionArgs, String sortOrder)

Uri: The URI of the content provider to query

Projection: List of columns to return

Selection: SQL WHERE clause

selectionArgs: The arguments to selection

sortOrder: SQL ORDER BY clause

A query returns a set of zero or more database
records

The retrieved data is exposed by a Cursor object

Cursor can be used to iterate backward or forward
through the result set

Cursor can be used only to read the data

Data kept by a content provider can be
modified by :
 Adding new records
 Adding new values to existing records
 Batch updating existing records
 Deleting records

Set up a map of key-value pairs in a ContentValues
object
 Each key matches the name of a column in the content
provider
 The value is the desired value for the new record in that
column
Call ContentResolver.insert and pass it the URI of
the provider and the ContentValues map
 This method returns the full URI of the new record

 i.e. the provider's URI with the appended ID for the new
record



Once a record exists, you can add new
information to it or modify existing
information
The best way to add to a record in the
database is to append the name of the table
where the new data goes to the URI for the
record
then use the amended URI to add the new
data values

To update a single record:
 ContentResolver.update with the URI of a specific
row

To update multiple rows:
 ContentResolver.update with the URI of the type
of record to delete and an SQL WHERE clause
defining which rows to update
public final int update
(Uri uri, ContentValues values, String where,
String[] selectionArgs)

Values: The new field values
 A null value will remove an existing field value


Where: A filter to apply to rows before
deleting, formatted as an SQL WHERE clause
(excluding the WHERE itself)
Returns The number of rows updated.

To delete a single record:
 ContentResolver.delete with the URI of a specific
row

To delete multiple rows:
 ContentResolver.delete with the URI of the type
of record to delete and an SQL WHERE clause
defining which rows to delete
Int android.content.ContentResolver.delete
(Uri url, String where, String[] selectionArgs)
url: the url of the row/table to delete
 where: A filter to apply to rows before
deleting, formatted as an SQL WHERE clause
(excluding the WHERE itself)
 selectionArgs: arguments for the where clause
 Returns the number of rows deleted

To create a content provider:
 Set up a system for storing the data .
 Most content providers store their data using
Android's file storage methods or SQLite
databases


Extend the ContentProvider class to provide
access to the data.
Declare the content provider in the manifest
file for your application


Subclass ContentProvider
Implementing six abstract methods:
 query)(
 insert()
 update()
 delete()
 getType()
 onCreate()


The query method must return a Cursor object that can
iterate over the requested data
Cursor itself is an interface, but Android provides some
ready-made Cursor objects that you can use
 For example ,SQLiteCursor can iterate over data stored in an
SQLite database

ContentProvider methods can be called from various
ContentResolver objects in different processes and
threads, they must be implemented in a thread-safe
manner!

Define CONTENT_URI constants for each of the subtables
 content//:com.example.codelab.transporationprovider/train
 content//:com.example.codelab.transporationprovider/air/domestic
 content//:com.example.codelab.transporationprovider/air/inter
List running devices: adb devices
 Open a shell to a device: adb shell
 location of all shell commands: /system/bin
 list with permissions: Ls –l
 list of databases packages: ls /data/data
 List contact databases: ls
/data/data/com.android.providers.contacts/databases
 pull contacts. db down to your local machine:

 adb pull
/data/data/com.android.providers.contacts/databases/con
tacts.db c:/somelocaldir/contacts.db


http://www.sqlite.org/
SQLite databases on the device are created
as needed







Open: sqlite3
/data/data/com.android.providers.contacts/databases/con
tacts.db
Exit: .exit
List the tables: .tables
prints out a create statement for a table: .schema <table>
Set the column headers to show in the tool
sqlite>.headers on
select all rows from a table: select * from table1;
count the number of rows in a table: select count(*) from
table1;






select a specific set of columns: select col1, col2 from
table1;
Select distinct values in a column: select distinct col1 from
table1;
counting the distinct values: select count(col1) from
(select distinct col1 from table1);
group by: select count(*), col1 from table1 group by col1;
regular inner join: select * from table1 t1, table2 t2 where
t1.col1 = t2.col1;
left outer join, Give me everything in t1 even though there
are no rows in t2: select * from table t1 left outer join
table2 t2 on t1.col1 = t2.col1 where ....