Sie sind auf Seite 1von 3

SQLite is an open-source relational database i.e.

used to perform database


operations on android devices such as storing, manipulating or retrieving persistent data
from the database.

SQLiteOpenHelper class
The android.database.sqlite.SQLiteOpenHelper class is used for database creation and
version management. For performing any database operation, you have to provide the
implementation of onCreate() andonUpgrade() methods of SQLiteOpenHelper class.

Methods of SQLiteOpenHelper class


There are many methods in SQLiteOpenHelper class. Some of them are as follows:
Method

Description

public abstract void onCreate(SQLiteDatabase db)

called only once when database is


created for the first time.

public abstract void onUpgrade(SQLiteDatabase db, int

called when database needs to be

oldVersion, int newVersion)

upgraded.

public synchronized void close ()

closes the database object.

public void onDowngrade(SQLiteDatabase db, int

called when database needs to be

oldVersion, int newVersion)

downgraded.

SQLiteDatabase class It contains methods to be performed on sqlite


database such as create, update, delete, select etc.

Methods of SQLiteDatabase class


There are many methods in SQLiteDatabase class. Some of them are as follows:
Method

Description

void execSQL(String sql)

executes the sql query not select query.

long insert(String table, String

inserts a record on the database. The table specifies the

nullColumnHack, ContentValues

table name, nullColumnHack doesn't allow completely

values)

null values. If second argument is null, android will store


null values if values are empty. The third argument
specifies the values to be stored.

int update(String table,

updates a row.

ContentValues values, String


whereClause, String[] whereArgs)
Cursor query(String table, String[]

returns a cursor over the resultset.

columns, String selection, String[]


selectionArgs, String groupBy,
String having, String orderBy)

Content Providers & Content Resolvers


The android.content Package
The android.content package contains classes for accessing and publishing data. The
Android framework enforces a robust and secure data sharing model. Applications
are notallowed direct access to other application's internal data. Two classes in the
package help enforce this requirement: the ContentResolver and the ContentProvider.

What is the Content Resolver?


The Content Resolver is the single, global instance in your application that provides
access to your (and other applications') content providers. The Content Resolver
behaves exactly as its name implies: it accepts requests from clients,
and resolves these requests by directing them to the content provider with a distinct
authority. To do this, the Content Resolver stores a mapping from authorities to Content
Providers. This design is important, as it allows a simple and secure means of accessing
other applications' Content Providers.
The Content Resolver includes the CRUD (create, read, update, delete) methods
corresponding to the abstract methods (insert, query, update, delete) in the Content
Provider class. The Content Resolver does not know the implementation of the Content
Providers it is interacting with (nor does it need to know); each method is passed an URI
that specifies the Content Provider to interact with.

What is a Content Provider?


Whereas the Content Resolver provides an abstraction from the application's Content
Providers, Content Providers provide an abstraction from the underlying data source (i.e.
a SQLite database). They provide mechanisms for defining data security (i.e. by
enforcing read/write permissions) and offer a standard interface that connects data in
one process with code running in another process.
Content Providers provide an interface for publishing and consuming data, based around
a simple URI addressing model using the content:// schema. They enable you to
decouple your application layers from the underlying data layers, making your
application data-source agnostic by abstracting the underlying data source.

The Life of a Query


So what exactly is the step-by-step process behind a simple query? As described above,
when you query data from your database via the content provider, you don't
communicate with the provider directly. Instead, you use the Content Resolver object to
communicate with the provider. The specific sequence of events that occurs when a
query is made is given below:
1.

A call to getContentResolver().query(Uri, String, String, String, String) is made.


The call invokes the Content
Resolver's query method, not the ContentProvider's.
2.
When the query method is invoked, the Content Resolver parses
the uri argument and extracts its authority.
3.
The Content Resolver directs the request to the content provider
registered with the (unique) authority. This is done by calling the Content
Provider's query method.
4.
When the Content Provider's query method is invoked, the query is
performed and a Cursor is returned (or an exception is thrown). The
resulting behavior depends entirely on the Content Provider's
implementation.

Das könnte Ihnen auch gefallen