Code example for SQLiteDatabase

Methods: beginTransactionendTransactionreplacesetTransactionSuccessful

0
     * @param value the value (can be null) 
     * @throws CacheException when key is null 
     */ 
    public void writeData(String key, String value) throws CacheException {
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        db.beginTransaction();
        try { 
            writeDataLocked(db, key, value);
            db.setTransactionSuccessful();
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                Log.i(TAG, "Wrote (key, value) = [ " + key + ", " + value + "] ");
            } 
        } finally { 
            db.endTransaction();
        } 
    } 
 
    /** 
     * Write a (key, value) pair in the database used by the cache. This call should be called into 
     * a transaction. 
     * 
     * @param db the database (must not be null) 
     * @param key the key (must not be null) 
     * @param value the value 
     * @throws CacheException when key or database are null 
     */ 
    protected void writeDataLocked(SQLiteDatabase db, String key, String value)
            throws CacheException { 
        if (null == db) {
            throw new CacheException("Database cannot be null"); 
        } 
        if (null == key) {
            throw new CacheException("Cannot use null key for write"); 
        } 
 
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME_ID, key.hashCode());
        values.put(COLUMN_NAME_KEY, key);
        values.put(COLUMN_NAME_VALUE, value);
 
        db.replace(DATABASE_NAME, null /* null column hack */, values);
    } 
 
    /** 
     * Read a value from the database used by the cache and depending on a key. 
     *