Sie sind auf Seite 1von 25

SCLlLe uaLabases

COMP 355 (Muppala) Data Storage 1


SCLlLe uaLabase
Androld appllcauons can have appllcauon daLabases powered by
SCLlLe
LlghLwelghL and le-based, ldeal for moblle devlces
uaLabases are prlvaLe for Lhe appllcauon LhaL creaLes Lhem
uaLabases should noL be used Lo sLore les
SCLlLe ls a llghL welghL daLabase
ALomlc
SLable
lndependenL
Lndurlng
Cnly several kllobyLes
Cnly parLly supporL some SCL commands such as AL1L8, 1A8LL.
SCLlLe ls lncluded as parL of Androld's soware sLack
More lnfo abouL SCLlLe aL hup://www.sqllLe.org
COMP 355 (Muppala) Data Storage 2
SCLlLe uaLabases
SLeps for uslng SCLlLe daLabases:
1. CreaLe a daLabase
2. Cpen Lhe daLabase
3. CreaLe a Lable
4. CreaLe and lnserL lnLerface for daLaseLs
3. CreaLe a query lnLerface for daLaseLs
6. Close Lhe daLabase
Cood pracuce Lo creaLe a uaLabase AdapLer class Lo slmpllfy
your daLabase lnLeracuons
We wlll use Lhe SCLlLe daLabase dened ln Lhe noLebook
LuLorlal as an example
COMP 355 (Muppala) Data Storage 3
SCLlLe Lxample: noLebook 1uLorlal
!"#$%& &$()) *+,-).#/0(!,-1 2

!"#$%& ),(3& 45($ 6,1%57 !"#$%&%'" ) *+,-.*/
!"#$%& ),(3& 45($ 6,1%57 !"#$012# ) *3456*/
!"#$%& ),(3& 45($ 6,1%57 !"#$718&2 ) *$95*/

!1%8(,- ),(3& 45($ 6,1%57 %:; ) *<4,.=23:5>?,.@*/
!1%8(,- .(,(#()-9-$!-1 :.#9-$!-1;
!1%8(,- 6<=%,-.(,(#()- :.#;

/**
* uaLabase creauon sql sLaLemenL
*/
!1%8(,- ),(3& 45($ 6,1%57 2:%:0:A"$B7":%" )
"creaLe Lable noLes (_ld lnLeger prlmary key auLolncremenL, "
+ "uLle LexL noL null, body LexL noL null),",

!1%8(,- ),(3& 45($ 6,1%57 2:%:0:A"$<:C" ) *5>,>*/
!1%8(,- ),(3& 45($ 6,1%57 2:%:0:A"$%:0'" ) *D4,.=*/
!1%8(,- ),(3& 45($ %5, 2:%:0:A"$E"7A&1< ) F/

!1%8(,- 45($ >+5,-?, :>,?;
COMP 355 (Muppala) Data Storage 4
SCLlLeCpenPelper Class
AbsLracL class for lmplemenung a besL pracuce pauern for creaung,
openlng and upgradlng daLabases
1o creaLe a SCLlLe daLabase, Lhe recommended approach ls Lo creaLe a
subclass of SCLlLeCpenPelper class
1hen overrlde lLs onCreaLe() meLhod
1hen execuLe a SCLlLe command Lo creaLe Lables ln Lhe daLabase
use Lhe onupgrade() meLhod Lo handle upgrade of Lhe daLabase
A slmple way would be Lo drop an exlsung Lable and replace wlLh a new defenluon
8euer Lo mlgraLe exlsung daLa lnLo a new Lable
1hen use an lnsLance of Lhe helper class Lo manage openlng or upgradlng
Lhe daLabase
lf Lhe daLabase doesn'L exlsL, Lhe helper wlll creaLe one by calllng lLs onCreaLe()
handler
lf Lhe daLabase verslon has changed, lL wlll upgrade by calllng Lhe onupgrade()
handler
COMP 355 (Muppala) Data Storage 5
SCLlLe Lxample: noLebook 1uLorlal
!1%8(,- ),(3& &$()) .(,(#()-9-$!-1 -?,-50) 6<=%,-@!-59-$!-1 2

uaLabasePelper(ConLexL conLexL) [
)"!-1A&+5,-?,B 2:%:0:A"$<:C"G DH--G 2:%:0:A"$E"7A&1<I/
}

[Cverrlde
!"#$%& 8+%0 +5>1-(,-A6<=%,-.(,(#()- 0#C 2

db.execSCL(!"#"$"%&'()&"#&*+
}

[Cverrlde
!"#$%& 8+%0 +5D!71(0-A6<=%,-.(,(#()- 0#B %5, +$0E-1)%+5B %5, 5-FE-1)%+5C 2
Log.-.#"/0 1234567894 76:6;6<= >5?@ A=5<8?9 1 B ?C7D=5<8?9 B 1 :? 1
+ newverslon + ", whlch wlll desLroy all old daLa"),
db.execSCL("u8C 1A8LL ll LxlS1S noLes"),
onCreaLe(db),
}
}
COMP 355 (Muppala) Data Storage 6
SCLlLe Lxample: noLebook 1uLorlal
!"#$%& *+,-).#/0(!,-1A>+5,-?, &,?C 2
,G%)H:>,? I &,?;
}

!"#$%& *+,-).#/0(!,-1 +!-5AC ,G1+F) 6<=J?&-!3+5 2
mubPelper = 5-F .(,(#()-9-$!-1A:>,?C;
mub = mubPelper.geLWrlLableuaLabase(),
1-,"15 ,G%);
}

!"#$%& 8+%0 &$+)-AC 2
mubPelper.close(),
}
COMP 355 (Muppala) Data Storage 7
SCLlLe uaLabases
ConLenLvalues() ob[ecLs used Lo hold rows Lo be lnserLed lnLo Lhe daLabase
Lxample:
!"#$%& $+57 &1-(,-*+,-A6,1%57 3,$-B 6,1%57 #+0KC 2
ConLenLvalues lnlualvalues = 5-F >+5,-5,E($"-)AC;
lnlualvalues.puL(E&F'#G#H&0 I:C=*+
lnlualvalues.puL(E&F'$J!F0 ;?7K*+

1-,"15 :.#H%5)-1,A2:%:0:A"$%:0'"G DH--G 9D9+>-E>-H.=I/
}

!"#$%& #++$-(5 0-$-,-*+,-A$+57 1+FL0C 2

reLurn mub.deleLe(!"#"$"%&'#"$H&0 E&F')JLG! B 1M1 B 5?-G70 9NCC* O P+
}
!"#$%& #++$-(5 "!0(,-*+,-A$+57 1+FL0B 6,1%57 3,$-B 6,1%57 #+0KC 2
ConLenLvalues args = 5-F >+5,-5,E($"-)AC;
args.puL(E&F'#G#H&0 I:C=*+
args.puL(E&F'$J!F0 ;?7K*+

1-,"15 :.#H"!0(,-A2:%:0:A"$%:0'"G >@J=G !"#$718&2 K *)* K @4L&5G DH--I M N/
}
COMP 355 (Muppala) Data Storage 8
SCLlLe uaLabases
uaLabase querles are reLurned as Cursor ob[ecLs
olnLers Lo Lhe resulung seLs wlLhln Lhe underlylng daLa
Cursor class provldes several meLhods:
move1ollrsL, move1onexL, move1orevlous, move1oosluon used Lo
move Lo a row
geLCounL Lo geL Lhe number of rows ln Lhe cursor
geLosluon Lo geL Lhe currenL row posluon
geLColumnname, geLColumnnames, geLColumnlndexor1hrow Lo geL
lnfo on columns
sLarLManaglngCursor and sLopManaglngCursor meLhods used Lo
lnLegraLe cursor llfeume lnLo Lhe acuvlLy's llfeume
COMP 355 (Muppala) Data Storage 9
SCLlLe Lxample: noLebook 1uLorlal
!"#$%& >"1)+1 M-,&G/$$*+,-)AC 2

1-,"15 :.#HN"-1KA2:%:0:A"$%:0'"G D.L A,@9DJOP Q!"#$718&2G !"#$%&%'"G
E&F'$J!FQ0 DH--G DH--G DH--G DH--G DH--I/
}
!"#$%& >"1)+1 M-,&G*+,-A$+57 1+FL0C ,G1+F) 6<=J?&-!3+5 2

Cursor mCursor =

mub.query(,1"-B 2:%:0:A"$%:0'"G D.L A,@9DJOP Q!"#$718&2G
E&F'#G#H&0 E&F'$J!FQ0 E&F')JLG! B 1M1 B 5?-G70 DH--G
5"$$B 5"$$B 5"$$B 5"$$C;
%M A:>"1)+1 OI 5"$$C 2
mCursor.move1ollrsL(),
}
1-,"15 :>"1)+1;

}
COMP 355 (Muppala) Data Storage 10
SCLlLe Lxample: noLebook 1uLorlal
WlLhln Lhe maln acuvlLy, cursors reLurned by Lhe ubadapLer are used as
follows:
!1%8(,- 8+%0 4$$.(,(AC 2
Cursor noLesCursor = mubPelper.feLchAllnoLes(),
sLarLManaglngCursor(noLesCursor),

// CreaLe an array Lo speclfy Lhe elds we wanL Lo dlsplay ln Lhe llsL (only 1l1LL)
SLrlng[] from = 5-F 6,1%57PQ2*+,-).#/0(!,-1H!"#$%&%'"R/

// and an array of Lhe elds we wanL Lo blnd Lhose elds Lo (ln Lhls case [usL LexL1)
%5,PQ ,+ I 5-F %5,PQ2RH%0H,.S,TR/

// now creaLe a slmple cursor adapLer and seL lL Lo dlsplay
SlmpleCursorAdapLer noLes =
5-F 6%:!$->"1)+1/0(!,-1A,G%)B RH$(K+",HD4,.=$@4LG D4,.=BH@=4@G U@4VG ,4I/
seLLlsLAdapLer(noLes),
}

COMP 355 (Muppala) Data Storage 11
ConLenL rovlders
COMP 355 (Muppala) Data Storage 12
ConLenL rovlders
SLore and reLrleve daLa and make lL avallable Lo all appllcauons
Cnly way Lo share daLa across appllcauons
SLandard conLenL provlders parL of Androld:
Common daLa Lypes (audlo, vldeo, lmages, personal conLacL lnformauon)
Appllcauons can creaLe Lhelr own conLenL provlders Lo make Lhelr
daLa publlc
AlLernauvely add Lhe daLa Lo an exlsung provlder
lmplemenL a common lnLerface for querylng Lhe provlder, addlng,
alLerlng and deleung daLa
AcLual sLorage of daLa ls up Lo Lhe deslgner
rovldes a clean separauon beLween Lhe appllcauon layer and daLa
layer
COMP 355 (Muppala) Data Storage 13
Accesslng ConLenL
Appllcauons access Lhe conLenL Lhrough a ConLenL8esolver
lnsLance
ConLenL8esolver allows querylng, lnserung, deleung and updaung
daLa from Lhe conLenL provlder
ConLenL8esolver cr = geLConLenL8esolver(),

cr.query(eople.CCn1Ln1_u8l, null, null, null, null), //querylng conLacLs

ConLenLvalues newvalues = new ConLenLvalues(),
cr.lnserL(eople.CCn1Ln1_u8l, newvalues),

cr.deleLe(eople.CCn1Ln1_u8l, null, null), //deleLe all conLacLs
COMP 355 (Muppala) Data Storage 14
ConLenL rovlders
ConLenL provlders expose Lhelr daLa as a slmple Lable on a
daLabase model
Lach row ls a record and each column ls daLa of a parucular Lype and
meanlng
Cuerles reLurn cursor ob[ecLs
Lach conLenL provlder exposes a publlc u8l LhaL unlquely
ldenues lLs daLa seL
SeparaLe u8l for each daLa seL under Lhe conLrol of Lhe provlder
u8ls sLarL wlLh conLenL://.
1yplcal formaL:
ConLenL://<package name>.provlder.<cusLom provlder name>/<uaLaaLh>
COMP 355 (Muppala) Data Storage 15
ConLenL rovlders: Cuery
?ou need Lhree pleces of lnformauon Lo query a conLenL provlder:
1he u8l LhaL ldenues Lhe provlder
1he names of Lhe daLa elds you wanL Lo recelve
1he daLa Lypes for Lhose elds
lf you're querylng a parucular record, you also need Lhe lu for LhaL record
Lxample:
lmporL androld.provlder.ConLacLs.eople,
lmporL androld.conLenL.ConLenLurls,
lmporL androld.neL.url,
lmporL androld.daLabase.Cursor,

// use Lhe ConLenLurls meLhod Lo produce Lhe base u8l for Lhe conLacL wlLh _lu == 23.
url myerson = ConLenLurls.wlLhAppendedld(eople.CCn1Ln1_u8l, 23),

// AlLernauvely, use Lhe url meLhod Lo produce Lhe base u8l.
// lL Lakes a sLrlng raLher Lhan an lnLeger.
url myerson = url.wlLhAppendedaLh(eople.CCn1Ln1_u8l, "23"),

// 1hen query for Lhls speclc record:
Cursor cur = managedCuery(myerson, null, null, null, null),
COMP 355 (Muppala) Data Storage 16
ConLenL rovlders: Cuery
lmporL androld.provlder.ConLacLs.eople,
lmporL androld.daLabase.Cursor,

// lorm an array speclfylng whlch columns Lo reLurn.
SLrlng[] pro[ecuon = new SLrlng[] [
eople._lu,
eople._CCun1,
eople.nAML,
eople.nuM8L8
},

// CeL Lhe base u8l for Lhe eople Lable ln Lhe ConLacLs conLenL provlder.
url conLacLs = eople.CCn1Ln1_u8l,

// Make Lhe query.
Cursor managedCursor = managedCuery(conLacLs,
pro[ecuon, // Whlch columns Lo reLurn
null, // Whlch rows Lo reLurn (all rows)
null, // Selecuon argumenLs (none)
// uL Lhe resulLs ln ascendlng order by name
eople.nAML + " ASC"),
COMP 355 (Muppala) Data Storage 17
ConLenL rovlders: Cuery
8eLrlevlng Lhe daLa:
lmporL androld.provlder.ConLacLs.eople,

prlvaLe vold geLColumnuaLa(Cursor cur)[
lf (cur.move1ollrsL()) [

SLrlng name,
SLrlng phonenumber,
lnL nameColumn = cur.geLColumnlndex(eople.nAML),
lnL phoneColumn = cur.geLColumnlndex(eople.nuM8L8),
SLrlng lmageaLh,

do [
// CeL Lhe eld values
name = cur.geLSLrlng(nameColumn),
phonenumber = cur.geLSLrlng(phoneColumn),

// uo someLhlng wlLh Lhe values.
...

} whlle (cur.move1onexL()),

}
}
COMP 355 (Muppala) Data Storage 18
ConLenL rovlders: Modlfylng uaLa
uaLa kepL by a conLenL provlder can be modled by:
Addlng new records
Addlng new values Lo exlsung records
8aLch updaung exlsung records
ueleung records
All accompllshed uslng ConLenL8esolver meLhods
use ConLenLvalues() Lo add or updaLe daLa
COMP 355 (Muppala) Data Storage 19
ConLenL rovlders: Addlng uaLa
Addlng new records:
lmporL androld.provlder.ConLacLs.eople,
lmporL androld.conLenL.ConLenL8esolver,
lmporL androld.conLenL.ConLenLvalues,

ConLenLvalues values = new ConLenLvalues(),

// Add Abraham Llncoln Lo conLacLs and make hlm a favorlLe.
values.puL(eople.nAML, "Abraham Llncoln"),
// 1 = Lhe new conLacL ls added Lo favorlLes
// 0 = Lhe new conLacL ls noL added Lo favorlLes
values.puL(eople.S1A88Lu, 1),

url url = geLConLenL8esolver().lnserL(eople.CCn1Ln1_u8l, values),
COMP 355 (Muppala) Data Storage 20
ConLenL rovlders: Addlng uaLa
Addlng new values:
url phoneurl = null,
url emallurl = null,

phoneurl = url.wlLhAppendedaLh(url, eople.hones.CCn1Ln1_ul8LC1C8?),

values.clear(),
values.puL(eople.hones.1?L, eople.hones.1?L_MC8lLL),
values.puL(eople.hones.nuM8L8, "1233214367"),
geLConLenL8esolver().lnserL(phoneurl, values),

// now add an emall address ln Lhe same way.
emallurl = url.wlLhAppendedaLh(url, eople.ConLacLMeLhods.CCn1Ln1_ul8LC1C8?),

values.clear(),
// ConLacLMeLhods.klnu ls used Lo dlsungulsh dlerenL klnds of
// conLacL meLhods, such as emall, lM, eLc.
values.puL(eople.ConLacLMeLhods.klnu, ConLacLs.klnu_LMAlL),
values.puL(eople.ConLacLMeLhods.uA1A, "LesL[example.com"),
values.puL(eople.ConLacLMeLhods.1?L, eople.ConLacLMeLhods.1?L_PCML),
geLConLenL8esolver().lnserL(emallurl, values),
COMP 355 (Muppala) Data Storage 21
ConLenL rovlders
use ConLenL8esolver.updaLe() Lo baLch updaLe elds
use ConLenL8esolver.deleLe() Lo deleLe:
A speclc row
Muluple rows, by calllng Lhe meLhod wlLh Lhe u8l of Lhe Lype of
record Lo deleLe and an SCL WPL8L clause denlng whlch rows Lo
deleLe
COMP 355 (Muppala) Data Storage 22
Creaung ConLenL rovlders
1o creaLe a conLenL provlder, you musL:
SeL up a sysLem for sLorlng Lhe daLa. MosL conLenL provlders sLore
Lhelr daLa uslng Androld's le sLorage meLhods or SCLlLe daLabases,
buL you can sLore your daLa any way you wanL.
LxLend Lhe ConLenLrovlder class Lo provlde access Lo your daLa
ueclare Lhe conLenL provlder ln Lhe manlfesL le for your appllcauon
(AndroldManlfesL.xml)
COMP 355 (Muppala) Data Storage 23
Creaung ConLenL rovlders
LxLendlng Lhe ConLenLrovlder class wlll requlre:
lmplemenung Lhe followlng meLhods: query(), lnserL(), updaLe(),
deleLe(), geL1ype(), onCreaLe()
Make sure LhaL Lhese lmplemenLauons are Lhread-safe as Lhey may be
called from several ConLenL8esolver ob[ecLs ln several dlerenL
processes and Lhreads
ln addluon, you need Lo:
ueclare a publlc sLauc nal u8l named CCn1Ln1_u8l
uene Lhe column names LhaL Lhe conLenL provlder wlll reLurn Lo
cllenLs
Carefully documenL Lhe daLa Lype of each column
COMP 355 (Muppala) Data Storage 24
Creaung ConLenL rovlders
?ou need Lo declare Lhe conLenL provlder ln Lhe ManlfesL le:
Lxample:
<provlder androld:name="com.example.auLos.AuLolnforovlder"
androld:auLhorlues="com.example.auLos.auLolnfoprovlder"
. . . />
</provlder>
COMP 355 (Muppala) Data Storage 25

Das könnte Ihnen auch gefallen