0 Bewertungen0% fanden dieses Dokument nützlich (0 Abstimmungen)
17 Ansichten25 Seiten
SCLlLe databases are lightweight, file-based databases ideal for mobile devices. They are private to each application and should not be used to store files. The steps for using SCLlTe databases include creating a database, opening it, creating tables, and inserting and querying data. A SQLiteOpenHelper class can simplify interactions with the database. The example tutorial uses a SQLiteOpenHelper subclass to manage database creation and upgrading. Queries return Cursor objects to access result sets. Content providers make application data available to other apps through a common interface.
SCLlLe databases are lightweight, file-based databases ideal for mobile devices. They are private to each application and should not be used to store files. The steps for using SCLlTe databases include creating a database, opening it, creating tables, and inserting and querying data. A SQLiteOpenHelper class can simplify interactions with the database. The example tutorial uses a SQLiteOpenHelper subclass to manage database creation and upgrading. Queries return Cursor objects to access result sets. Content providers make application data available to other apps through a common interface.
Copyright:
Attribution Non-Commercial (BY-NC)
Verfügbare Formate
Als PDF, TXT herunterladen oder online auf Scribd lesen
SCLlLe databases are lightweight, file-based databases ideal for mobile devices. They are private to each application and should not be used to store files. The steps for using SCLlTe databases include creating a database, opening it, creating tables, and inserting and querying data. A SQLiteOpenHelper class can simplify interactions with the database. The example tutorial uses a SQLiteOpenHelper subclass to manage database creation and upgrading. Queries return Cursor objects to access result sets. Content providers make application data available to other apps through a common interface.
Copyright:
Attribution Non-Commercial (BY-NC)
Verfügbare Formate
Als PDF, TXT herunterladen oder online auf Scribd lesen
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
!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
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
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(),
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,
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),
// 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