9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.
-
Upload
marybeth-wilkerson -
Category
Documents
-
view
225 -
download
0
Transcript of 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.
![Page 1: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/1.jpg)
9 Persistence - SQLite
CSNB544 Mobile Application Development
Thanks to Utexas Austin
![Page 2: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/2.jpg)
2
Databases• RDBMS– relational data base management system
• Relational databases introduced by E. F. Codd– Turing Award Winner
• Relational Database–data stored in tables– relationships among data stored in tables–data can be accessed and view in
different ways
![Page 3: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/3.jpg)
3
SQL and SQLite• Structured Query Language• programming language to manage data
in a RDBMS• SQLite implements most,
but not all of SQL• SQLite becomes part of application
![Page 4: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/4.jpg)
4
SQLite and Android• Databases created with or for application
accessible by name to all classes in application, but none outside application
• Creating database:– create subclass of SQLiteOpenHelper and
override onCreate() method–execute SQLite command to create tables in
database
![Page 5: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/5.jpg)
5
Creating Database• Example: Movie Rating App• Stores user ratings• Not a complex example• Database only has one table• Adapted from Deitel Address Book
Application• http://www.deitel.com/Books/Android/
AndroidforProgrammers/tabid/3606/Default.aspx
![Page 6: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/6.jpg)
6
ClassesMovieRaterActivity
Starting ActivityDisplays List of RatedMovies
AddEditRatingAdd or Edit Rating
menu - Add Rating
ViewRatingShow Rating
and Information
click on Movie Title
menu - Edit Rating
menu - Delete Rating
Row remove from database
DatabaseConnectorInteract With Database
![Page 7: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/7.jpg)
7
MovieRaterActivity• ScrollView• Queries data base for
all names / titles• Clicking on Title
brings up that rating in ViewRating
![Page 8: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/8.jpg)
8
Menu for MovieRaterActivity• Only one menu
option• button to Add
Rating• Brings up
AddEditRating Activity
![Page 9: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/9.jpg)
9
ViewRating• Pulls all data from
database for row based on name / title
• Use of a RatingBar• ViewRating has its
own Menu
![Page 10: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/10.jpg)
10
ViewRating Menu• Edit Rating starts
AddEditRating activity and populates fields with these values (place in Extras)
• Delete Rating brings up confirmation Dialog
![Page 11: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/11.jpg)
11
AddEditRating• Add Rating–fields are blank
• Consider adding a button for date picker instead of typing data
• Must enter title / name
• other fields can be blank
![Page 12: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/12.jpg)
12
AddEditRating• When title clicked in
main Activity, MovieRaterActivity
• Make changes and click save
![Page 13: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/13.jpg)
13
DatabaseConnector Class• Start of class
![Page 14: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/14.jpg)
14
DatabaseConnector Class
![Page 15: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/15.jpg)
15
Creating Database• Via an inner class that extends
SQLiteOpenHelper
![Page 16: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/16.jpg)
16
Creating Database• Money method
![Page 17: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/17.jpg)
17
Creating Database• String is a SQLite command• ratings is name of table• table has seven columns–_id, name, genre, dateSeen, tag1, tag2,
rating• storage classes for columns:– TEXT, INTEGER, REAL– also NULL and BLOB
• _id is used as primary key for rows
![Page 18: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/18.jpg)
18
Database on Device
• can pull database and view• sqlitebrowser is a good tool
![Page 19: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/19.jpg)
19
Inserting Data• ContentValues are key/value pairs that
are used when inserting/updating databases
• Each ContentValue object corresponds to one row in a table
• _id being added and incremeneted automatically
![Page 20: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/20.jpg)
20
Inserting Data• In AddEditRating• When save button clicked
![Page 21: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/21.jpg)
21
Inserting Data• In DatabaseConnector
nullCoumnHack, for inserting empty row
![Page 22: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/22.jpg)
22
Updating Data• In AddEditRating• When save button clicked• notice id added
![Page 23: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/23.jpg)
23
Updating Data• In DatabaseConnector
![Page 24: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/24.jpg)
24
Query Data• Getting a single row by _id– in order to populate ViewRating
![Page 25: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/25.jpg)
25
Query Data• Get all rows• To populate the ListView in the
MovieRaterActivity• only getting _id and name columns
![Page 26: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/26.jpg)
26
Deleting Data• Menu Option in ViewRating
![Page 27: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/27.jpg)
27
Database Cursor• Cursor objects allow random read - write
access to the result of a database query• Ours only used to read the data• Use a CursorAdapter to map columns
from cursor to TextView or ImageViews defined in XML files
![Page 28: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/28.jpg)
28
Database Connection• Recall:
![Page 29: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/29.jpg)
29
MovieRaterActivity• Rating Adapter is a CursorAdapter• from onCreate method
![Page 30: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/30.jpg)
30
Updating Cursor• Cursor initially null• separate task to create cursor and
update adapter
![Page 31: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/31.jpg)
31
Asynch Task
![Page 32: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/32.jpg)
32
Clicking on Item in List• _id not displayed but still part of entry in
list -> use _id to get back to database row
![Page 33: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/33.jpg)
33
Other Cursor Options• moveToPrevious• getCount• getColumnIndexOrThrow• getColumnName• getColumnNames• moveToPosition• getPosition
![Page 34: 9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.](https://reader035.fdocuments.net/reader035/viewer/2022062408/56649ee65503460f94bf5926/html5/thumbnails/34.jpg)
34
Possible Upgrades• Add functionality to – show all movies that share a particular
genre–movies from a date range– shared tags
• Just more complex data base queries