M - Apps5 - Saving Persisting Data
M - Apps5 - Saving Persisting Data
IS457
Mobile Applications
Lecture 5
Saving Persisting Data
1
1/18/2021
SQL Languages
2
1/18/2021
SQLite Database
• Pros
• Very light weight database
• Quick to sort and retrieve large data sets
• Can easily be used to store customized data
• Can enforce relationships and integrity between data
• Cons
• Uses space limited internal device memory
SQLite Database
• When an application creates a database it is saved in:
• DATA/data/APP_NAME/databases/FILENAME
• Where
3
1/18/2021
SQLite Database
• Imports
• android.database
contains all necessary classes
• android.database.sqlite
contains SQLite specific classes
4
1/18/2021
SQLite Database
• Subclass SQLiteOpenHelper to create and upgrade database.
• In the constructor you invoke superclass with your database name and current
version.
• Override onCreate() and onUpgrade().
• SQLiteOpenHelper provides programmer with:
- getReadableDatabase()
- getWriteableDatabase() methods.
• Use _id attribute as (autoincrement) primary key
SQLiteOpenHelper Example
Subclass SQLiteOpenHelper to create
and upgrade database
private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_CONTACTS + "
(" + COLUMN_ID +
" integer primary key autoincrement," + COLUMN_NAMES + " TEXT NOT
NULL);";
5
1/18/2021
SQLiteOpenHelper
In the constructor you invoke superclass
with your database name and current
version
SQLite Database
• SQLiteDatabase provide
- Insert()
- Update()
- Delete()
- execSQL()
6
1/18/2021
7
1/18/2021
• Provides:
- getCount()
- moveToFirst()
- moveToNext()
database = dbHelper.getWritableDatabase();
return this;}
8
1/18/2021
Shared Preference
• General framework in Android that will allow you to save/retrieve key value pairs for
primitive objects such as boolean, float, int, long and string.
• Pros
• Really built-in to the existing Android framework, so easy to use, and easy to create a
preference activity to view and edit the preferences
• Quick
• Can really be used to store any data with manipulation with the string type.
• Cons
• Not suitable for storing very large amounts of data, as it will be slow in retrieving and
sorting through the data.
• Use internal device memory (more limited than external)
• Requires customization for any non-primitive data types.
9
1/18/2021
editor.putString("name", “Sarah");
editor.putInt(“age", 12);
editor.commit();
10
1/18/2021
• Example
11