Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Chapter Six:

Working with Files and Database

Damana D.

1
SQLite Database

• Is an open source database


• Supports standard relational database features
• Android comes in with built in SQLite database
implementation
• To create and upgrade a database in your android
application you create a subclass of the
SQLiteOpenHelper class.
• In the constructor of your subclass you call the
super() method of SQLiteOpenHelper

2
• SQLite used to perform database operations on android
devices such as
– storing,
– manipulating or retrieving persistent data from the database.
• SQLiteOpenHelper class provides the functionality to
use the SQLite database.

3
SQLiteOpenHelper class

• used for database creation and version management.


• For performing any database operation, you have to
provide the implementation
of onCreate() and onUpgrade() methods of
SQLiteOpenHelper class.

4
Constructors of SQLiteOpenHelper class

• creates an object for creating, opening and managing


the database.

5
Methods of SQLiteOpenHelper class

• There are many methods in SQLiteOpenHelper class.


Some of them are as follows:
Method Description

public abstract void called only once when database


onCreate(SQLiteDatabase db) is created for the first time.
public abstract void called when database needs to
onUpgrade(SQLiteDatabase db, be upgraded.
int oldVersion, int newVersion)

public synchronized void close () closes the database object.

public void called when database needs to


onDowngrade(SQLiteDatabase be downgraded.
db, int oldVersion, int
newVersion) 6
SQLiteDatabase class

• It contains methods to be performed on sqlite database


such as create, update, delete, select etc.
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
nullColumnHack, ContentValues values) specifies the table name, nullColumnHack
doesn't allow completely 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, ContentValues updates a row.


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)

7
Steps to create SQLite Database
• Create a new java class called=DatabaseHelper
• Extend it with SQLiteOpenHelper
• Implement all the methods
• Example table: student
ID Name Mark
1 Ahmed 75
2 Lili 88
3 Beti 65

• Now lets create the database inside the constructor


public DatabaseHelper( Context context ){
Super(context, databaseName,null,1);

} 8
Table
• To create a table: use onCreate(….) method

public void onCreate(SQLiteDatabase sqLiteDatabase) {

• To drop a table: use onUpgrade(…….) method

public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i,


int i1) {

}
9
ContentValues

• ContentValues is a name value pair,


• used to insert or update values into database tables. 
• ContentValues object will be passed to
SQLiteDataBase objects insert() and update()
functions.
• Cursor is a temporary buffer area which stores
results from a SQLiteDataBase query.

10
Exercise

11
Thank You

?
12

You might also like