[go: up one dir, main page]

0% found this document useful (0 votes)
72 views14 pages

Mobile Programming: Chapter Five Working With Data storage/SQLITE Database

The document discusses working with SQLite databases in Android applications. It introduces the SQLiteOpenHelper class for creating and managing databases and the SQLiteDatabase class for performing operations like insert, update, delete on the database. It explains that SQLiteOpenHelper provides methods to create and upgrade the database structure and SQLiteDatabase provides methods to execute SQL queries and retrieve data from the database. An example at the end demonstrates creating a DatabaseHelper class that extends SQLiteOpenHelper to manage the database for an Android application.

Uploaded by

Hiziki Tare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views14 pages

Mobile Programming: Chapter Five Working With Data storage/SQLITE Database

The document discusses working with SQLite databases in Android applications. It introduces the SQLiteOpenHelper class for creating and managing databases and the SQLiteDatabase class for performing operations like insert, update, delete on the database. It explains that SQLiteOpenHelper provides methods to create and upgrade the database structure and SQLiteDatabase provides methods to execute SQL queries and retrieve data from the database. An example at the end demonstrates creating a DatabaseHelper class that extends SQLiteOpenHelper to manage the database for an Android application.

Uploaded by

Hiziki Tare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Mobile Programming

CHAPTER FIVE

Working with data storage/SQLITE


database
Outline
2

Introduction
SQLiteOpenHelper class
SQLiteDatabase class
Brainstorming
3

Q1. What do you do when you write a GUI java


program that inserts data in database?
steps
1. ?
2. ??
3. ???
4. ………….
Introduction
4

Android provides several ways to store user


and app data.
SQLite is one way of storing user data.
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.
It is embedded in android by default.
Con’t …
5

Using does not require a setup or administration.


You only have to define the SQL statements for
creating and updating the database.
Afterwards the database is automatically managed
for you by the Android platform.
SQLite supports the data types TEXT (similar to
String in Java), INTEGER (similar to long in Java)
and REAL (similar to double in Java).
All other types must be converted into one of
these fields before getting saved in the database.
Packages
6

1. The android.database package contains all


necessary classes for working with databases.
2. The android.database.sqlite package
contains the SQLite specific classes.
Brainstorming
7

Q2. What do you do when you need to perform


the following operations in database?

1. Create Database
2. Delete Database
3. Insert into table
4. Deleting from table
5. Update table
SQLiteOpenHelper Class
8

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,
specifying the database name and the current
database version.
You need to override the methods to create and
update your database.
Constructors
9

1. SQLiteOpenHelper(Context context, String


name, SQLiteDatabase.CursorFactory factory,
int version)
creates an object for creating, opening and
managing the database.
2. SQLiteOpenHelper(Context context, String
name, SQLiteDatabase.CursorFactory factory, int
version, DatabaseErrorHandler errorHandler)
creates an object for creating, opening and
managing the database. It specifies the error
handler.
Methods
10

onCreate() - called only once when database is


created for the first time.
onUpgrade() - called when database needs to
be upgraded.
The SQLiteOpenHelper class provides the
getReadableDatabase() and
getWriteableDatabase() methods to get access
to an SQLiteDatabase object; either in read or
write mode.
SQLiteDatabase class
11

It contains methods to be performed on SQLite


database such as
create,
update,
delete,
select etc…
Methods
12

 void execSQL(String sql) executes the sql query not


select query.
 long insert(String table, String nullColumnHack,
ContentValues values) inserts a record on the
database.
 int update(String table, ContentValues values,
String whereClause, String[] whereArgs) updates a
row.
 Cursor query(String table, String[] columns, String
selection, String[] selectionArgs, String groupBy,
String having, String orderBy) returns a cursor over
the resultset.
Brainstorming
13

Where the layout file is located?


How to create a class?
How the layout designed is loaded in ?
Example
14

Steps
1. create a new android project SQLiteApp
2. create class DatabaseHelper inside source
package.
3.DatabaseHelper extends
SQLiteOpenHelper
4.Write constructor and methods of super
class
5.Do operations like insert, update,…..

You might also like