Wednesday, May 5, 2010

Content Providers and SQLite

The content provider abstraction allows you to retrieve data using REST-like syntax. For example, to retrieve  book 69 from the book database, create a URI like:

       content://com.android.book.BookProvider/books/23

Content-provider abstraction is required only if you want to share data (files, SQL data, etc) externally or between applications.  

SQLite is built into the Android runtime, so Android applications  can use SQLite databases.

If you  are using the SQLite database only internally, you need  a class that extends SQLiteOpenHelper, then create an instance and ask it to getReadableDatabase() or getWriteableDatabase().  

      db=(new myDBClass(this)).getWritableDatabase();

Use db.execSQL(...) to execute SQL statements such as insert, update and delete that does not return values, or db.insert(), db.delete(), db.update() can be used.  The syntax is not JDBC but uses a simpler API.

Use db.rawQuery(..) if the select query is fixed, otherwise db.query(..), which returns a cursor. Another way is to use SQLiteQueryBuilder interface to encapsulate the call details.

No comments:

Post a Comment