Upload
brian-dorsey
View
225
Download
0
Embed Size (px)
Citation preview
RAVI GAURAV PANDEY 1
SQLite DBStoring Data in Android
RAVI GAURAV PANDEY 2
Storing Data on Android PlatformFour 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 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 4
SQLite DataTypes
This is quite different than the normal SQL data types so please read:
Storage Class Description
NULL The 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.
REALThe value is a floating point value, stored as an 8-byte IEEE floating point number.
TEXTThe 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.
RAVI GAURAV PANDEY 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 affinitiesAffinity Description
TEXTThis column stores all data using storage classes NULL, TEXT or BLOB.
NUMERICThis column may contain values using all five storage classes.
INTEGERBehaves 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.
RAVI GAURAV PANDEY 6
SQLite Packages & Classes(android.database.sqli
te)
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 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 8
1. Creating a Database
SQLiteDatabase sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, 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 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 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 11
3. Inserting Values
sampleDB.execSQL("INSERT INTO " +
SAMPLE_TABLE_NAME +
" Values (‘Tom',‘Patterson',‘UK',28);");
RAVI GAURAV PANDEY 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 13
5. Display the values as List
this.setListAdapter(new ArrayAdapter<String>(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 14
6. Deleting the values
finally {
if (sampleDB != null)
sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
sampleDB.close();
}
RAVI GAURAV PANDEY 15
QUESTIONS?