$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

    View Slide

  2. Content Providers
    Managers of structured data
    Abstract underlying data storage
    Allow external access to data

    View Slide

  3. Manage connection to data source

    View Slide

  4. Easy access to data

    View Slide

  5. Implicit intents

    View Slide

  6. Loader callbacks

    View Slide

  7. Provide data to external apps

    View Slide

  8. PROBLEMS
    — And we had to fix them

    View Slide

  9. Boilerplate code

    View Slide

  10. DB versioning

    View Slide

  11. JSON to SQL mapping

    View Slide

  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);
    }

    View Slide

  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);
    }

    View Slide

  14. Upgrade it
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion == 0) {
    db.execSQL("DROP TABLE article");
    createDatabase(db);
    }
    }

    View Slide

  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);

    View Slide

  16. Our solution
    https://github.com/novoda/SQLiteProvider

    View Slide