17
DATABASE IN ANDROID FIRST PART

Database in Android

Embed Size (px)

DESCRIPTION

Database in Android pplication development.

Citation preview

Page 1: Database in Android

DATABASE IN ANDROIDFIRST PART

Page 2: Database in Android

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

Page 3: Database in Android

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?

Page 4: Database in Android

● 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

Page 5: Database in Android

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

Page 6: Database in Android

1. Create subclass of SQLiteOpenHelper class.

The method of Creation DB

Page 7: Database in Android

2.Execute the constructor of SQLiteOpenHelper class by

constructor of the class created in 1, create database.

The method of Creation DB

Page 8: Database in Android

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

Page 9: Database in Android

4. Execute the SQL statement that creates Table at

SQLiteOpenHelper#onCREATE.

The method of creation DB

Page 10: Database in Android

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:

Page 11: Database in Android

@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):

Page 12: Database in Android

@Override

public void onUpgrade

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

}

}

Example(continue):

Page 13: Database in Android

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

Page 14: Database in Android

● Get SQliteOpenDatabase objects by SQLiteOpenHelper method.

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

● getReadableDatabase.

● getWritableDatabase.

Search data from DB

Page 15: Database in Android

● 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

Page 16: Database in Android

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)

Page 17: Database in Android

// Transfer the Cursor object management to Activity

startManagingCursor( cursor);

}finally{

if(database)= null){

//disconnect with database

database.close();

}

}

Example( query method)