Anda di halaman 1dari 28

Lecture 8

DATABASE IN ANDROID

Data Storage Options in Android


Shared Preferences Store private primitive data in key-value pairs.

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

Saving To Shared Preferences


SharedPreferences settings = getSharedPreferences(PREF_NAME, 0); SharedPreferences.Editor editor = settings.edit();

editor.putBoolean(MyData", false);
editor.commit(); // Commit the edits

PREF_NAME is the name of the shared pref file. String

Retrieve Shared preferences data


SharedPreferences myPref = getSharedPreferences(PREF_NAME, 0); myPref.getTYPE(Name,RETURN_IF_NOT_EXSITS); PREF_NAME is the name of the shared pref file. String

Clearing Preferences
From Code preferencesEditor.clear(); preferencesEditor.commit();

By Users From Device


On your device, if you go to Settings > Applications > Manage Applications > Your App youll see a button to Clear Data. Clicking this clears any data your app has saved, including SharedPreferences: preferencesEditor.clear(); preferencesEditor.commit();

Using Internal Storage


Saved on the device's internal storage When the user uninstalls your application, these files are removed.

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 :

Checking Media Availability

Google Documentation Reference


http://developer.android.com/guide/topics/data/data-storage.html

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

SQLite and Android


Android provides full support for SQLite databases. Database created are private to your application.

Contacts Example

Table Structure

SQlite Open Helper

All CRUD Operations (Create, Read, Update and Delete)

Inserting new Record

Reading Row(s) with Conditions

Reading Row(s)

Rows Count

Updating Record

Deleting Record

Using the database in an activity

Anda mungkin juga menyukai