$30 off During Our Annual Pro Sale. View Details »

SQLite Provider: Database access made easy

SQLite Provider: Database access made easy

David González

January 29, 2014
Tweet

More Decks by David González

Other Decks in Technology

Transcript

  1. SQLite Provider: A simplification of database access David Gonzalez

  2. Content Providers Managers of structured data Abstract underlying data storage

    Allow external access to data
  3. Manage connection to data source

  4. Easy access to data

  5. Implicit intents

  6. Loader callbacks

  7. Provide data to external apps

  8. PROBLEMS — And we had to fix them

  9. Boilerplate code

  10. DB versioning

  11. JSON to SQL mapping

  12. Create the database public ExampleDatabase(Context context, CursorFactory factory, int version)

    { super(context, DATABASE_NAME, factory, version); } @Override public void onCreate(SQLiteDatabase db) { createDatabase(db); }
  13. protected void createDatabase(SQLiteDatabase db) { final String articleTable = "CREATE

    TABLE articles" // + " ('" // + _id + "' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, '" // + title + "' TEXT, '" // + content + "' TEXT, '" // + timestamp + "' TIMESTAMP NOT NULL );"; db.execSQL(articleTable); @Override public void onCreate(SQLiteDatabase db) { createDatabase(db); }
  14. Upgrade it @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int

    newVersion) { if (oldVersion == 0) { db.execSQL("DROP TABLE article"); createDatabase(db); } }
  15. Upgrade it ContentResolver resolver = getContentResolver(); String[] projection = new

    String[] {UserDictionary.Words._ID, UserDictionary.Words.WORD}; String selectionClause = UserDictionary.Words.WORD + " = ?"; String[] selectionArgs = new String[] {"Android"}; String sortOrder = UserDictionary.Words.WORD + " ASC”; Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, projection, selectionClause, selectionArgs, sortOrder);
  16. Our solution https://github.com/novoda/SQLiteProvider