Lecture 8 Database in Android

download Lecture 8 Database in Android

of 28

Transcript of Lecture 8 Database in Android

  • 8/13/2019 Lecture 8 Database in Android

    1/28

    Lecture 8DATABASE IN ANDROID

  • 8/13/2019 Lecture 8 Database in Android

    2/28

    Data Storage Options in Android

    Shared PreferencesStore 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.

  • 8/13/2019 Lecture 8 Database in Android

    3/28

    Shared PreferencesProvides a general framework that allows you to save and retrieve persistent key-vprimitive data types.

    To get a shared preferences object, use one of those methods

  • 8/13/2019 Lecture 8 Database in Android

    4/28

    Shared Preferencespublic abstract SharedPreferences getSharedPreferences (String name, int mode)

    Retrieve and hold the contents of the preferences file 'name', returning a SharedPthrough which you can retrieve and modify its values.

    mode represent permissions and could be : MODE_PRIVATE, MODE_WORLD_REAMODE_WORLD_WRITEABLE

  • 8/13/2019 Lecture 8 Database in Android

    5/28

    Saving To Shared PreferencesSharedPreferences 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

  • 8/13/2019 Lecture 8 Database in Android

    6/28

    Retrieve Shared preferences datSharedPreferences myPref = getSharedPreferences(PREF_NAME, 0);myPref.getTYPE(Name,RETURN_IF_NOT_EXSITS);

    PREF_NAME is the name of the shared pref file. String

  • 8/13/2019 Lecture 8 Database in Android

    7/28

    Clearing PreferencesFrom Code

    preferencesEditor.clear(); preferencesEditor.commit();

    By Users From Device

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

  • 8/13/2019 Lecture 8 Database in Android

    8/28

    Using Internal StorageSaved 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

  • 8/13/2019 Lecture 8 Database in Android

    9/28

    Writing to SD CardFrom previous session we used:

    OutputStream output = new FileOutputStream("/mnt/sdcard/image2.jpg");

    Use getExternalFilesDir() to open a File that represents the external storage directyou 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

  • 8/13/2019 Lecture 8 Database in Android

    10/28

    Writing to SDCard

    Helper Methods :

  • 8/13/2019 Lecture 8 Database in Android

    11/28

    Checking Media Availability

  • 8/13/2019 Lecture 8 Database in Android

    12/28

    Google Documentation Referenchttp://developer.android.com/guide/topics/data/data-storage.html

    http://developer.android.com/guide/topics/data/data-storage.htmlhttp://developer.android.com/guide/topics/data/data-storage.htmlhttp://developer.android.com/guide/topics/data/data-storage.htmlhttp://developer.android.com/guide/topics/data/data-storage.htmlhttp://developer.android.com/guide/topics/data/data-storage.html
  • 8/13/2019 Lecture 8 Database in Android

    13/28

    SQLite

  • 8/13/2019 Lecture 8 Database in Android

    14/28

    SQLiteSQLite is a lightweight transactional database engine that occupies a small amoun

    storage and memory.

    It is a software library that implements a self-contained, serverless, zero-configuratransactional SQL database engine. http://www.sqlite.org/

    You use SQLite to manipulate databases, create, update, delete and insert data in tables

  • 8/13/2019 Lecture 8 Database in Android

    15/28

    Using SQLiteStandard 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 examptext and vice versa

    Refer to SQLite limits in the official documentation here : http://www.sqlite.org/li

  • 8/13/2019 Lecture 8 Database in Android

    16/28

    SQLite and AndroidAndroid provides full support for SQLite databases.

    Database created are private to your application.

  • 8/13/2019 Lecture 8 Database in Android

    17/28

    Contacts Example

  • 8/13/2019 Lecture 8 Database in Android

    18/28

    Table Structure

  • 8/13/2019 Lecture 8 Database in Android

    19/28

  • 8/13/2019 Lecture 8 Database in Android

    20/28

    SQlite Open Helper

  • 8/13/2019 Lecture 8 Database in Android

    21/28

  • 8/13/2019 Lecture 8 Database in Android

    22/28

    Inserting new Record

  • 8/13/2019 Lecture 8 Database in Android

    23/28

    Reading Row(s) with Conditions

  • 8/13/2019 Lecture 8 Database in Android

    24/28

    Reading Row(s)

  • 8/13/2019 Lecture 8 Database in Android

    25/28

    Rows Count

  • 8/13/2019 Lecture 8 Database in Android

    26/28

    Updating Record

  • 8/13/2019 Lecture 8 Database in Android

    27/28

    Deleting Record

  • 8/13/2019 Lecture 8 Database in Android

    28/28

    Using the database in an activity