Local Database in Nebula

bawltherapistSoftware and s/w Development

Dec 13, 2013 (3 years and 8 months ago)

112 views

L
ocal Database in

Nebula
v1.0

Page
1

of
9


sdfasfasdf



Development partner:

Project Team

Marco Pierobon (15)

marco87c@hotmail.it

Michel Hognerud (30)

hognerud@kth.se

Nina Mulkijanyan (24)

ninamu@kth.se

Prajwol Kumar Nakarmi (30)

nakarmi@kth.se

Sharique Javaid (15)

sharique@kth.se

Sujan Dey (15)

sujan@kth.se

Syed Saad Ali (30)

ssali3@kth.se

Venkatesh Change Gowda (24)

vch@kth.se

Project Owners

Niklas Lindhe

niklas@xtecom.com


Project Coach

Hans Eriksson

hansa@me.com



Project Champion

Björn Pehrson

bpehrson@kth.se


Local Database in Nebula

Version 1.0

2011
.
01
.
05

L
ocal Database in

Nebula
v1.0

Page
2

of
9


Revision History

Version

Date

Remark
s

Authors

1.0

2011
-
01
-
05

Document creat
ion

Formatted by Sharique Javaid

Sujan Dey























L
ocal Database in

Nebula
v1.0

Page
3

of
9


Table of Contents

Revision History

................................
................................
................................
................................
......

2

1

Introduction

................................
................................
................................
................................
....

4

1.1

Purpose of t
he document

................................
................................
................................
...........

4

1.2

Scope

................................
................................
................................
................................
..........

4

2

About SQLite Database

................................
................................
................................
...................

5

3

SQLite in Android

................................
................................
................................
............................

6

3.1

Classes

................................
................................
................................
................................
.....

6

4

SQLite in Nebula app
lication

................................
................................
................................
...........

8

5

References

................................
................................
................................
................................
......

9






















L
ocal Database in

Nebula
v1.0

Page
4

of
9


1

Introduction

1.1

Purpose of the document

This document is the guide for the developers how to set up a
SQLite

database for android
programming. It also provides a guide to make a
n

android application using
SQLite

database.


1.2

Scope

The scope of the document covers the development of application using SQLite database in
android programming
.




















L
ocal Database in

Nebula
v1.0

Page
5

of
9


2

About
SQLite

Database

SQLite [1]

is
an

in
-
process library that implements a self
-
contained, server less, zero
-
configuration SQL database engine. The
SQLite

is free for use for any purpose, commercial
or private.
SQLite

is currently found in more applicati
ons than we can count, including
several high
-
profile projects.

SQLite

is an embedded SQL database engine. Unlike most other SQL databases,
SQLite

does
not have a separate server process.
SQLite

reads and writes directly to ordinary disk files.
The databas
e file format is cross
-
platform, can be used freely in operating systems these
features make
SQLite

a popular choice as an Application File Format. It also uses very less
memory which makes it a popular database engine choice on memory constrained gadgets
such as cell phones, PDAs, and MP3 players.




















L
ocal Database in

Nebula
v1.0

Page
6

of
9


3

SQLite

in Android

Android uses the
SQLite

database system, which is an open
-
source, stand
-
alone SQL
database, widely used by many popular applications.


In Android, the database that you create for an application is only accessible to itself; other
applications will not be able to access it. Once created, the
SQLite

database is stored in the
/data/data/<package_name>/databases folder of an Android device.

In android,
the
android.database.
SQLite

[2] package

contains the
SQLite

database
management classes that an application would use to manage its own private database.


3.1

Classes

SQLite
Closable

An object created from a
SQLite
Database that can be closed.


SQLite
Cursor

A Cursor implementation that exposes results from a query on a
SQLite
Database
.


SQLite
Database

Exposes methods to manage a
SQLite

database.


SQLite
OpenHelper

A helper class to manage database creation and version
management.


SQLite
Program

A base class for compiled
SQLite

programs.


SQLite
Query

A
SQLite

program that represents a query that reads the resulting
rows into a CursorWindow.


SQLit
e
QueryBuilder

This is a convience class that helps build SQL queries to be sent to
SQLite
Database

objects.


SQLite
Statement

A pre
-
compiled statement against a
SQLite
Database

that can be
reused.


A subclass implementing
onCreate(
SQLite
Database)
,
onUpgrade(
SQLite
Database,int,int)
and
optionally
onOpen(
SQLite
Database) methods in the
SQLite
OpenHelper ,takes care of
opening the database if it exists, creating it if it does not, and upgrading it as necessary.
Transactions are used to make sure the database is always in a sensible stat
e.

SQLite
OpenHelper

[3]

class is the helper class to manage database creation and version
management.



L
ocal Database in

Nebula
v1.0

Page
7

of
9


Public Methods in
SQLite
OpenHelper:


-

OnCreate

(
SQLite
Database db)


This

method is called when the database is created for the first time in an
application
. This is
the place where the tables are
created and

the population of
values of

the tables should
happen.

-

OnOpen

(
SQLite
Database db)


It is c
alled when the database has been opened. The implementation should check
isReadOnly()

before updating

the database

-

OnUpgrade

(
SQLite
Database db, int oldVersion, int newVersion)


This

method is called when the database needs to be upgraded. The implementation should
use this method to drop tables, add tables, or do anything else it needs to upgrade to th
e
new schema version.

















L
ocal Database in

Nebula
v1.0

Page
8

of
9


4

SQLite

in Nebula application

In Nebula application,
SQLite

database is used to store the local values like storing password
for a user so that when next time he/she opens the application, it can automatically retrieve
the password for that user. It is also used to store the audio setting for a specific user. So
t
hat, when a user logs in to the application he/she can get the desire audio setting.

For this,

a local
SQLite

database named “NebulaDB” with a table has been created with
fields _id(PK),username(String),password(String),ischecked(String),volume(String).

A
class “NebulaLocalDB” has been created which extends
the
SQLite
OpenHelper class

an
Android helper class for database creation and versioning management.

T
he onCreate()
method in this class creates a new database if the required database is not present. The

onUpgrade() method is called when the database needs to be upgraded. This is achieved by
checking the value defined in the DATABASE_VERSION constant.

Android uses the Cursor class as a return value for queries. The cursor act as a pointer to the
result se
t from a database query. Using Cursor allows Android to more efficiently manage
rows and columns as and when needed. The Content Values object is used to store
key/value pairs.
It’s

put (
) method allows
inserting

keys with values of different data types.















L
ocal Database in

Nebula
v1.0

Page
9

of
9


5

References

[1]

SQLite

website

[
Online]. Available:
http://www.
SQLite
.org

[2]

Android Developers
,
android.database.sqlite

[
Online].
Available:

http://developer.android.com/reference/android/database/sqlite/package
-
summary.html

[3]

Android Developers
,
SQLiteOpenHelper

[Online]. Available:
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html