SQLite (part deux) 1 CS440
Traditional Model View Controller (MVC) CS440 2
Android: Database-centric data CS440 3
Useful classes SQLiteDatabase is the base class for working with a SQLite database in Android. Methods: open query: rawQuery(), execSQL() update close CS440 4 Cursor cursor = getReadableDatabase(). rawQuery("select * from todo where _id = ?", new String[] { id });
Cursor A query returns a Cursor object Cursor can represent many objects (multiple lines) A Cursor points to one row of the query result You can move between multiple lines with Cursor.moveToNext() Useful methods: getCount() moveToFirst(), moveToNext(), moveToPrevious() isAfterLast() getAs*(): eg. getAsString(int columnNumber) 5
ListViews, ListActivities, and SimpleCursorAdapter ListViews : Views which allow to display a list of elements ListActivities : specialized Activities which make the usage of ListViews easier SimpleCursorAdapter : maps the columns to the Views based on the Cursor passed to it CS440 6
SQLiteOpenHelper Provides lifecycle framework for creating and upgrading your application database Useful methods: String getDatabaseName() void onOpen(SQLiteDatabase db) abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) More on: database/sqlite/SQLiteOpenHelper.html database/sqlite/SQLiteOpenHelper.html CS440 7
SQLiteQueryBuilder High level abstraction for creating SQL queries Useful Methods: Cursor query(SQLiteDatabase db, String[] projectionIn, String s election, String[] selectionArgs, String groupBy, String h aving, String sortOrder)SQLiteDatabaseString[]StringString[]String Perform a query by combining all current settings and the information passed into this method. String buildQuery(boolean distinct, String tables, String[] columns, String where, Stri ng groupBy, String having, String orderBy, String limit)StringString[]StringStri ngString CS440 8
References Oreilly, Programming Android, Mednieks, Dornin CS440 9