Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1.

Similar presentations


Presentation on theme: "SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1."— Presentation transcript:

1 SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1

2 Storing Data on Android Platform Four ways of storing data  Preferences  SQLite Database  Files  Network Preferences –  Basically used for storing user preferences for a single application or across applications for a mobile. This is typically name-value pairs accessible to the context. Databases –  Android supports creating of databases based on SQLite db. Each database is private to the applications that creates it Files –  Files can be directly stored on the mobile or on to an extended storage medium. By default other applications cannot access it. Network –  Data can be stored and retrieved from the network too depending on the availability.  If an application wants to store and retrieve data for its own use, without having to share the data across applications, it can access the SQLite DB directly. There is no need of a content provider. RAVI GAURAV PANDEY 2

3 SQLite DB Operations  Create a database (typically a one time activity)  Create a table (typically a one time activity)  Insert values into the table  Retrieve the values from the table  Display the retrieved values as a List view  Delete all the records from the table before closing the connection to the database RAVI GAURAV PANDEY 3

4 SQLite DataTypes  This is quite different than the normal SQL data types so please read: RAVI GAURAV PANDEY 4 Storage ClassDescription NULLThe value is a NULL value. INTEGER The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value. REAL The value is a floating point value, stored as an 8-byte IEEE floating point number. TEXT The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE) BLOBThe value is a blob of data, stored exactly as it was input.

5 Affinity Types  SQLite supports the concept of type affinity on columns. Any column can still store any type of data but the preferred storage class for a column is called its affinity. Each table column in an SQLite3 database is assigned one of the following type affinities RAVI GAURAV PANDEY 5 AffinityDescription TEXT This column stores all data using storage classes NULL, TEXT or BLOB. NUMERICThis column may contain values using all five storage classes. INTEGER Behaves the same as a column with NUMERIC affinity with an exception in a CAST expression. REAL Behaves like a column with NUMERIC affinity except that it forces integer values into floating point representation NONE A column with affinity NONE does not prefer one storage class over another and no attempt is made to coerce data from one storage class into another.

6 SQLite Packages & Classes ( android.database.sqlite ) The “sqlite” Contains the SQLite database management classes that an application would use to manage its own private database.  SQLiteCloseable - An object created from a SQLiteDatabase that can be closed.  SQLiteCursor - A Cursor implementation that exposes results from a query on a SQLiteDatabase.  SQLiteDatabase - Exposes methods to manage a SQLite database.  SQLiteOpenHelper - A helper class to manage database creation and version management.  SQLiteProgram - A base class for compiled SQLite programs.  SQLiteQuery - A SQLite program that represents a query that reads the resulting rows into a CursorWindow.  SQLiteQueryBuilder - a convenience class that helps build SQL queries to be sent to SQLiteDatabase objects.  SQLiteStatement - A pre-compiled statement against a SQLiteDatabase that can be reused. RAVI GAURAV PANDEY 6

7 android. database. sqlite. SQLiteDatabase  Contains the methods for: creating, opening, closing, inserting, updating, deleting and quering an SQLite database  These methods are similar to JDBC but more method oriented than what we see with JDBC (remember there is not a RDBMS server running) RAVI GAURAV PANDEY 7

8 1. Creating a Database SQLiteDatabase sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVA TE, null); This opens a database defined in the constant SAMPLE_DB_NAME, if it already exists. Else it creates a database and opens it. The second parameter is operating mode : MODE_PRIVATE meaning it is accessible to only this context. The other modes are and MODE_WORLD_WRITABLE. MODE_WORLD_READABLE RAVI GAURAV PANDEY 8

9 Database Properties  Important database configuration options include: version, locale, and thread-safe locking. import java.util.Locale; myDatabase.setVersion(1); myDatabase.setLockingEnabled(true); myDatabase.SetLocale(Locale.getDefault()); RAVI GAURAV PANDEY 9

10 2. Creating a Table sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " + SAMPLE_TABLE_NAME + " (LastName VARCHAR, FirstName VARCHAR," + " Country VARCHAR, Age INT(3));"); RAVI GAURAV PANDEY 10

11 3. Inserting Values sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values (‘Tom',‘Patterson',‘UK',28);"); RAVI GAURAV PANDEY 11

12 4. Retrieve a Value Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM " + SAMPLE_TABLE_NAME + " where Age > 10 LIMIT 5", null); if (c != null ) { if (c.moveToFirst()) { do { String firstName = c.getString(c.getColumnIndex("FirstName")); int age = c.getInt(c.getColumnIndex("Age")); results.add("" + firstName + ",Age: " + age); }while (c.moveToNext()); } RAVI GAURAV PANDEY 12

13 5. Display the values as List this.setListAdapter(new ArrayAdapter (this, android.R.layout.simple_list_item_1,results)); The statement displays it as a list as the class extends a ListActivity. RAVI GAURAV PANDEY 13

14 6. Deleting the values finally { if (sampleDB != null) sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME); sampleDB.close(); } RAVI GAURAV PANDEY 14

15 QUESTIONS? RAVI GAURAV PANDEY 15


Download ppt "SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1."

Similar presentations


Ads by Google