Lecture 8 Database in Android
Lecture 8 Database in Android
DATABASE IN ANDROID
Internal Storage
Store private data on the device memory. External Storage Store public data on the shared external storage. SQLite Databases Store structured data in a private(to application) database.
Shared Preferences
Provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. To get a shared preferences object, use one of those methods
Shared Preferences
public abstract SharedPreferences getSharedPreferences (String name, int mode)
Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values.
mode represent permissions and could be : MODE_PRIVATE, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE
editor.putBoolean(MyData", false);
editor.commit(); // Commit the edits
Clearing Preferences
From Code preferencesEditor.clear(); preferencesEditor.commit();
Files are stored in the file folder under you package directory in the device. /data/data/yourAPP/files
Writing to SD Card
From previous session we used: OutputStream output = new FileOutputStream("/mnt/sdcard/image2.jpg");
Use getExternalFilesDir() to open a File that represents the external storage directory where you should save your files. (API Level 8 or greater)
Parameter can be predefined path or null to get the root folder Predefined paths examples are : DIRECTORY_MUSIC | DIRECTORY_RINGTONES
Writing to SDCard
Helper Methods :
SQLite
SQLite
SQLite is a lightweight transactional database engine that occupies a small amount of disk storage and memory. It is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. http://www.sqlite.org/ You use SQLite to manipulate databases, create, update, delete and insert data in database tables
Using SQLite
Standard SQL Support for Triggers
Does not implement referential integrity constraints. Use Triggers to handle this
Null, Integer, Real and Text data types allowed Its possible to insert different types in the same column. You can insert, for example, integer in text and vice versa Refer to SQLite limits in the official documentation here : http://www.sqlite.org/limits.html
Contacts Example
Table Structure
Reading Row(s)
Rows Count
Updating Record
Deleting Record