Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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. 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); }
  2. 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); }
  3. Upgrade it @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int

    newVersion) { if (oldVersion == 0) { db.execSQL("DROP TABLE article"); createDatabase(db); } }
  4. 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);