13 DataStorage
13 DataStorage
Contents
● Shared Preferences
● Internal/External Files
● Sqlite
● Room database
Shared Preferences
- Shared Preferences allows activities and applications to keep
preferences, in the form of key-value pairs
- Android stores Shared Preferences settings as XML file in
shared_prefs folder under DATA/data/{application package} directory
- To get access to the preferences, we have APIs:
- getPreferences() : used from within your Activity, to access activity-
specific preferences
- getSharedPreferences() : used from within your Activity (or other
application Context), to access application-level preferences
Shared Preferences
- Implement:
- getSharedPreferences (String PREFS_NAME, int mode)
- Storing Data
Shared Preferences
- Retrieving Data
WORD_LIST_TABLE
_id word definition
1 "alpha" "first letter"
2 "beta" "second letter"
3 "alpha" "particle"
SQL basic operations
● Insert rows
● Delete rows
● Update values in rows
● Retrieve rows that meet given criteria
SQL Query
● SELECT word, description
FROM WORD_LIST_TABLE
WHERE word="alpha"
Generic
● SELECT columns
FROM table
WHERE column="value"
21
SELECT columns FROM table
● SELECT columns
○ Select the columns to return
○ Use * to return all columns
22
WHERE column="value"
23
AND, ORDER BY, LIMIT
24
Sample queries
25
More sample queries
26
Last sample query
27
rawQuery()
28
query()
29
Cursors
Queries always return a Cursor object
Cursor is an object interface that provides random read-write access to
the result set returned by a database query
⇒ Think of it as a pointer to table rows
30
Using sqlite
● SQLiteDatabase db = openOrCreateDatabase( "name", MODE_PRIVATE,
null); db.execSQL("SQL query");
● Methods:
○ – db.beginTransaction(), db.endTransaction()
○ – db.delete("table", "whereClause" , args)
○ – db.deleteDatabase(file)
○ – db.insert("table", null, values)
○ – db.query(...)
○ – db.rawQuery("SQL query", args)
○ – db.replace("table", null, values)
○ – db.update("table", values, "whereClause", args)
Using sqlite
● ContentValues can be optionally used as a level of abstraction for statements like
INSERT, UPDATE, REPLACE
public void onDowngrade(SQLiteDatabase db, int called when database needs to be downgraded.
oldVersion, int newVersion)
Using sqlite
Method Description
void execSQL(String sql) executes the sql query not select query.
long insert(String table, String nullColumnHack, ContentValues values) inserts a record on the database. The table specifies the table name,
nullColumnHack doesn't allow completely null values. If second argument is
null, android will store null values if values are empty. The third argument
specifies the values to be stored.
Cursor query(String table, String[] columns, String selection, String[] returns a cursor over the resultset.
selectionArgs, String groupBy, String having, String orderBy)
Room database
● Room is a robust SQL object
mapping library
// Room components
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
annotationProcessor "androidx.room:room-compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"
Using Room database
● Create Entity
Using Room database
● Create DAO
Using Room database
● Create database
○ Create public abstract class extending RoomDatabase
○ Annotate as @Database