Top Banner

of 46

Android and SQLite

Feb 17, 2018

Download

Documents

Yunikouma
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/23/2019 Android and SQLite

    1/46

    Week 6

    TableLayout and SQLite

  • 7/23/2019 Android and SQLite

    2/46

    Objectives

    TableLayout, TableRow

    Introduction to SQLite

    SQLite Java classes in Android

    SQLiteOen!eler SQLite"atabase

    #ontent$alues

    #ursor

    SQLite "atabase Tutorial Add%Select%&date%"elete

  • 7/23/2019 Android and SQLite

    3/46

    TABLE LAYOUT

  • 7/23/2019 Android and SQLite

    4/46

    TableLayout and TableRow

    'urose o( TableLayout is to or)ani*e &I ele+ents in a

    table (or+at ac- row in a TableRow is a TableRow instance

    TableRow is divided into cells, a cell contains a sin)lec-ild view

    .u+ber o( colu+ns is dictated by t-e row wit- +ostcolu+ns

    Widt- o( a colu+n is de/ned by t-e widest cell in t-at

    colu+n

    #olu+ns +ay be set as s-rinkable or stretc-able 0orbot-1 so t-ey c-an)e in si*e relative to t-e arent

    TableLayout

    A sin)le cell +ay be con/)ured to san +ultile colu+ns

  • 7/23/2019 Android and SQLite

    5/46

    T-e 2&I

  • 7/23/2019 Android and SQLite

    6/46

    T-e !ierarc-ical Tree o( t-e2&I

  • 7/23/2019 Android and SQLite

    7/46

    SQLITE

  • 7/23/2019 Android and SQLite

    8/46

    Introduction

    T-e use o( database is an essentialasect o( +ost alications

    Ran)in) (ro+ alications entirely datadriven, to t-ose si+ly need to stores+all a+ounts o( data

    'ersistent data stora)e is +orei+ortant takin) into transient li(ecycle

    o( an Android activity

    Android rovides SQLite bounded wit-Android OS, rovides to (acilitate

    ersistent stora)e

  • 7/23/2019 Android and SQLite

    9/46

    W-at is SQLite3

    SQLite is an e+bedded, R"45S

    5ost R"45S are standalone server rocesses runindeendently 0e), 5s SQL, Oracle, 5ySQL, etc1

    SQLite is re(erred to as e+bedded because it isrovided in t-e (or+ o( a library t-at is linked intoalications .o standalone "4 server runnin) in t-e back)round

    All "4 oerations are -andled internally wit-in t-ealication t-rou)- calls to (unctions o( SQLite Lib

    SQLite is written in #, Android S"7 rovides Javabased 8wraer9 around t-e "4 Inter(aceT-is consists o( a set o( classes to create and +ana)e "4

  • 7/23/2019 Android and SQLite

    10/46

    Tryin) SQLite on an A$"

    Android also rovides interactiveenviron+ent (or issuin) SQL co++ands(ro+ wit-in an adb s-ell sessionconnected to a runnin) Android A$"

    Once an A$" is runnin), use Ter+inal toconnect to t-e e+ulator usin): adb eshell

    SQLite databases are actually stored in adatabase /les on t-e /le syste+ o( t-eAndroid device

  • 7/23/2019 Android and SQLite

    11/46

    SQLite "4 at-

    4y de(ault, t-e /le syste+ at- (or t-e"4 /le is

    ), in an a wit- acka)e co+e;+le5y"4A

    /data/data/com.example.MyDBApp/databases/mydatabase.db

    /data/data//databases/.db

  • 7/23/2019 Android and SQLite

    12/46

    ANDROID SQLITE JAVACLASSES

  • 7/23/2019 Android and SQLite

    13/46

    #ursor

    'rovided to access to t-e results o( a database

    irst01: +oves to /rst row in t-e result

    +oveToLast01: +oves to t-e last row in t-e result

    +oveTo.e;t01: +oves to t-e ne;t row in t-e result

    +ove01: +oves by a seci/ed o?set (ro+ current osition

    )et@Tye01 B returns value o( seci/ed @Tye

  • 7/23/2019 Android and SQLite

    14/46

    SQLite"atabase

    It rovides t-e ri+ary inter(ace between t-e

    alication code and underlyin) SQLitedatabasesTo create%delete databases

    To er(or+ SQL based oerations on databases

    So+e key +et-ods o( t-is class are: insert01: inserts new row into database table

    delete01: deletes rows (ro+ a database table

  • 7/23/2019 Android and SQLite

    15/46

    SQLiteOen!eler

    "esi)ned to +ake it easier to create and udate

    databases It +ust be subclassed wit-in t-e code o( t-e

    alication seekin) database access

    >ollowin) call back +et-ods +ust be i+le+ented on#reate01

    #alled w-en database is created (or t-e /rst ti+e

    It=s assed as ar)u+ent an SQLite"atabase object newlycreated

    T-is is ideal location to initiali*e "4 like creatin) table andinsertin) initial rows

    on&)rade01 #alled w-en a code contains +ore recent "4 version

    Tyically -aen w-en a is udated on t-e device

  • 7/23/2019 Android and SQLite

    16/46

    SQLiteOen!eler

    In addition to t-e +andatory callback +et-ods

    0listed reviously1, t-ere are so+e +ore +et-ods onOen01: called w-en "4 is oened

    T-e constructor (or t-e subclass +ust also bei+le+ented

    5ust call t-e suer class, assin) t-rou)- t-e alicationconte;t, t-e na+e o( t-e "4 and t-e "4 version

    .otable +et-ods o( SQLiteOen!eler class: )etWritable"atabase01: oen or creates a "4 (or readin)

    and writin) Returns a re(erence to t-e "4 )etReadable"atabase01: creates or oens a "4 (or

    readin) only Returns a re(erence to t-e "4

    close01: closes t-e database

  • 7/23/2019 Android and SQLite

    17/46

    #ontent$alues

    #ontent$alues is a convenience classt-at allows key%value airs to bedeclared

    #onsistin) o( table colu+n identi/ersand t-e values to be stored in eac-colu+n

    T-is class is o( articular use w-en

    insertin) or udatin) entries in adatabase table

  • 7/23/2019 Android and SQLite

    18/46

    SQLITE DATABASETUTORIAL

  • 7/23/2019 Android and SQLite

    19/46

    About t-e "4 e;a+le

    T-is roject is a si+le data entry andretrieval desi)ned to add,

  • 7/23/2019 Android and SQLite

    20/46

    T-e classes

    4esides t-e Activity class we will need

    A database -andler A subclass o( SQLiteOen!eler

    'rovides an abstract layer between t-e "4and activity

    A database +odel class to reresent t-e'roduct

  • 7/23/2019 Android and SQLite

    21/46

    #reate a roduct class

  • 7/23/2019 Android and SQLite

    22/46

    Select aroriate +in S"7

  • 7/23/2019 Android and SQLite

    23/46

    Add a 4lank Activity

  • 7/23/2019 Android and SQLite

    24/46

    Inut Activity In(or+ation

  • 7/23/2019 Android and SQLite

    25/46

    T-e "ata !andler

  • 7/23/2019 Android and SQLite

    26/46

    on#reate and on&)rade+et-ods

  • 7/23/2019 Android and SQLite

    27/46

    Add%Load !andler 5et-od

  • 7/23/2019 Android and SQLite

    28/46

    "elete 'roduct !andler 5et-od

  • 7/23/2019 Android and SQLite

    29/46

    &date 'roduct !andler5et-od

  • 7/23/2019 Android and SQLite

    30/46

    5ain Activity 2&I

  • 7/23/2019 Android and SQLite

    31/46

    5ain Activity Java #ode

  • 7/23/2019 Android and SQLite

    32/46

    Add Activity 2&I &sin)TableLayout

    dd i i d

  • 7/23/2019 Android and SQLite

    33/46

    Add Activity Java #ode

    - dd

  • 7/23/2019 Android and SQLite

    34/46

    Test t-e Add >eature

    L d A i i 2&I

  • 7/23/2019 Android and SQLite

    35/46

    Load Activity 2&I

    L t ( i l li t it

  • 7/23/2019 Android and SQLite

    36/46

    Layout (or sin)le list ite+

    L d A ti it J # d

  • 7/23/2019 Android and SQLite

    37/46

    Load Activity Java #ode

    T t T- L d > t

  • 7/23/2019 Android and SQLite

    38/46

    Test T-e Load >eature

    " l t %& d t 2&I

  • 7/23/2019 Android and SQLite

    39/46

    "elete%&date 2&I

    " l t & d t A ti it J

  • 7/23/2019 Android and SQLite

    40/46

    "elete &date Activity Java

    " l t & d t A ti it J

  • 7/23/2019 Android and SQLite

    41/46

    "elete &date Activity Java

    T t T- " l t > t

  • 7/23/2019 Android and SQLite

    42/46

    Test T-e "elete >eature

    & d t A ti it 2&I

  • 7/23/2019 Android and SQLite

    43/46

    &date Activity 2&I

    & d t A ti it J

  • 7/23/2019 Android and SQLite

    44/46

    &date Activity Java

    Test &date Acti it

  • 7/23/2019 Android and SQLite

    45/46

    Test &date Activity

    Su++aries

  • 7/23/2019 Android and SQLite

    46/46

    Su++aries

    TableLayout, TableRow

    Introduction to SQLite

    SQLite Java classes in Android SQLiteOen!eler

    SQLite"atabase

    #ontent$alues

    #ursor

    SQLite "atabase Tutorial Add%Select%&date%"elete