database in android

Post on 07-Jul-2015

504 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Database in Android pplication development.

TRANSCRIPT

DATABASE IN ANDROIDFIRST PART

If you want the app to remember data between

sessions, you need to persist the data.

How?

You can store your data using a SQLite

database.

Database Persistence

SQLite is a lightweight Database that :

● Use simple file database to store data.

● Can use SQL for Database operations.

● Can manage transactions.

● Specifications and source code are being published

● Driver have been developed in many languages.

What is SQLite?

● Thus, you can create and open database directly inside your app.

● The best way to get off the ground with a new database is to extend

a built in abstract base class called SQLiteOpenHelper that provides

you with all of the basic behavior to manage a database.

Start by creating the database

Create Database Perform:

Tables search/ insert/renew/delete

Create Table

Perform database connection: Execute the specified SQL-

(open/close)

Classes provided by Android

SQLiteHelper Class SQLiteDatabase Class

1. Create subclass of SQLiteOpenHelper class.

The method of Creation DB

2.Execute the constructor of SQLiteOpenHelper class by

constructor of the class created in 1, create database.

The method of Creation DB

3. Implement 2 methods that are decided by

SQLiteOpenHelper:

● onCreate: The method that implements the table creation

process.

● onUpgrade: The table that implements the table definition

renew process.

The method of creation DB

4. Execute the SQL statement that creates Table at

SQLiteOpenHelper#onCREATE.

The method of creation DB

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.util.log;

public class TrySQLiteOpenHelper extends SQLiteOpenHelper{

public trySQLiteOpenHelper(Context context){

super(context,”SAMPLE_DATABASE”, null, 1);

}

Example:

@override

public void onCreate(SQLiteDatabase database){

database.exec.SQL(“CREATE_TABLE_SAMPLE_TABLE

(-idINTEGER,

nameTT>T);”);

}

Note:

exec.SQL() executes the optional SQL statement to operate data.

Example(continue):

@Override

public void onUpgrade

(SQLite Database, int old version, int new version){

}

}

Example(continue):

The mechanism is :

● Need SQLiteDatabase objects to search data from database.

● By executing SQLiteDatabase#query method, the results can be

gotten by Cursor class.

Note:

Cursor is the class which represents a 2 dimensional table of any

database.

Search data from DB

● Get SQliteOpenDatabase objects by SQLiteOpenHelper method.

And, depend on uses, There’re 2 methods:

● getReadableDatabase.

● getWritableDatabase.

Search data from DB

● The method of search data:

1. Create the object of SQLiteOpenHelper class.

2. Use SQLiteOpenHelper#getReadable Database method to get

SQLiteDatabase objects.

3. Execute SQLiteDatabase#query method to get the Cursor objects

of search results.

4. Execute Activity#startManagingCursor to transmit Cursor

management to Activity.

5. Execute SQLiteDatabase#close method to close database.

SQLiteDatabase#query method

TrySQLiteOpenHelper database;

OpenHelper = new TrySQLiteOpenHelper(this);

SQLiteDatabase database = null;

Cursor cursor= null;

try{

// get the SQLiteDatabase objects.

database = database.OpenHelper.getReadableDatabase();

//get the cursor object of searching results.

cursor = database.query(“SAMPLE_TABLE”,null, null, null, null,

null);

Example(query method)

// Transfer the Cursor object management to Activity

startManagingCursor( cursor);

}finally{

if(database)= null){

//disconnect with database

database.close();

}

}

Example( query method)

top related