Code example for SQLiteOpenHelper

Methods: closegetWritableDatabase

0
     * @param helper 
     * @return writable database or null if it failed to create/open 
     */ 
    protected SQLiteDatabase getWritable(SQLiteOpenHelper helper) {
        try { 
            return helper.getWritableDatabase();
        } catch (SQLiteException e1) {
            // Make second attempt 
            try { 
                return helper.getWritableDatabase();
            } catch (SQLiteException e2) {
                return null; 
            } 
        } 
    } 
 
    /** 
     * Get readable database 
     * 
     * @param helper 
     * @return readable database or null if it failed to create/open 
     */ 
    protected SQLiteDatabase getReadable(SQLiteOpenHelper helper) {
        try { 
            return helper.getReadableDatabase();
        } catch (SQLiteException e1) {
            // Make second attempt 
            try { 
                return helper.getReadableDatabase();
            } catch (SQLiteException e2) {
                return null; 
            } 
        } 
    } 
 
    /** 
     * Load or request given resources 
     * 
     * @param persistableResource 
     * @return resource 
     * @throws IOException 
     */ 
    public <E> List<E> loadOrRequest(PersistableResource<E> persistableResource)
            throws IOException { 
        SQLiteOpenHelper helper = helperProvider.get();
        try { 
            List<E> items = loadFromDB(helper, persistableResource);
            if (items != null) {
                Log.d(TAG, "CACHE HIT: Found " + items.size() + " items for "
                        + persistableResource);
                return items;
            } 
            return requestAndStore(helper, persistableResource);
        } finally { 
            helper.close();
        } 
    } 
 
    /** 
     * Request and store given resources 
     * 
     * @param persistableResource 
     * @return resources 
     * @throws IOException 
     */ 
    public <E> List<E> requestAndStore(
            PersistableResource<E> persistableResource) throws IOException {
        SQLiteOpenHelper helper = helperProvider.get();
        try { 
            return requestAndStore(helper, persistableResource);
        } finally { 
            helper.close();
        } 
    } 
 
    private <E> List<E> requestAndStore(final SQLiteOpenHelper helper,
            final PersistableResource<E> persistableResource)