Code example for SQLiteDatabase

Methods: isOpenupdate

1
    private SQLiteDatabase mDatabase;
 
    @SuppressWarnings("deprecation") 
    public synchronized SQLiteDatabase getDatabase(Context context) {
        // Always return the cached database, if we've got one 
        if (mDatabase == null || !mDatabase.isOpen()) {
            DatabaseHelper helper = new DatabaseHelper(context, DATABASE_NAME);
            mDatabase = helper.getWritableDatabase();
            if (mDatabase != null) {
                mDatabase.setLockingEnabled(true);
            } 
		} 
 
        return mDatabase;
    } 
 
    private class DatabaseHelper extends SQLiteOpenHelper {
 
        DatabaseHelper(Context context, String name) {
            super(context, name, null, DATABASE_VERSION);
        } 
 
        @Override 
        public void onCreate(SQLiteDatabase db) {
            Log.d(LOG_TAG, "Creating PoCProvider database");
 
            // Create all tables here; each class has its own method 
            if (ACTIVATE_ALL_LOGS) {
                Log.d(LOG_TAG, "DbPerson | createTable start");
            } 
            DbPerson.createTable(db);
            if (ACTIVATE_ALL_LOGS) {
                Log.d(LOG_TAG, "DbPerson | createTable end");
            } 
        } 
 
        @Override 
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 
            // Upgrade all tables here; each class has its own method 
            if (ACTIVATE_ALL_LOGS) {
                Log.d(LOG_TAG, "DbPerson | upgradeTable start");
            } 
            DbPerson.upgradeTable(db, oldVersion, newVersion);
            if (ACTIVATE_ALL_LOGS) {
                Log.d(LOG_TAG, "DbPerson | upgradeTable end");
            } 
        } 
 
        @Override 
        public void onOpen(SQLiteDatabase db) {
        } 
    } 
 
    @Override 
    public int delete(Uri uri, String selection, String[] selectionArgs) {
 
        UriType uriType = matchUri(uri);
        Context context = getContext();
 
        // Pick the correct database for this operation 
        SQLiteDatabase db = getDatabase(context);
        String id = "0";
 
        if (ACTIVATE_ALL_LOGS) {
            Log.d(LOG_TAG, "delete: uri=" + uri + ", match is " + uriType.name());
        } 
 
        int result = -1;
 
        switch (uriType) {
            case DB_PERSON_ID:
                id = uri.getPathSegments().get(1);
                result = db.delete(uriType.getTableName(), whereWithId(id, selection), 
                        selectionArgs);
                break; 
            case DB_PERSON:
                result = db.delete(uriType.getTableName(), selection, selectionArgs);
                break; 
        } 
 
        getContext().getContentResolver().notifyChange(uri, null);
        return result;
    } 
 
    @Override 
    public String getType(Uri uri) {
        return matchUri(uri).getType();
    } 
 
    @Override 
    public Uri insert(Uri uri, ContentValues values) {
 
        UriType uriType = matchUri(uri);
        Context context = getContext();
 
        // Pick the correct database for this operation 
        SQLiteDatabase db = getDatabase(context);
        long id;
 
        if (ACTIVATE_ALL_LOGS) {
            Log.d(LOG_TAG, "insert: uri=" + uri + ", match is " + uriType.name());
        } 
 
        Uri resultUri = null;
 
        switch (uriType) {
            case DB_PERSON:
                id = db.insert(uriType.getTableName(), "foo", values);
                resultUri = id == -1 ? null : ContentUris.withAppendedId(uri, id);
                break; 
            default: 
                throw new IllegalArgumentException("Unknown URI " + uri);
        } 
 
        // Notify with the base uri, not the new uri (nobody is watching a new 
        // record) 
        getContext().getContentResolver().notifyChange(uri, null);
        return resultUri;
    } 
 
    @Override 
    public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) 
            throws OperationApplicationException { 
        SQLiteDatabase db = getDatabase(getContext());
        db.beginTransaction();
        try { 
            int numOperations = operations.size();
            ContentProviderResult[] results = new ContentProviderResult[numOperations];
            for (int i = 0; i < numOperations; i++) {
                results[i] = operations.get(i).apply(this, results, i);
                db.yieldIfContendedSafely();
            } 
            db.setTransactionSuccessful();
            return results;
        } finally { 
            db.endTransaction();
        } 
    } 
 
    @Override 
    public int bulkInsert(Uri uri, ContentValues[] values) {
 
        UriType uriType = matchUri(uri);
        Context context = getContext();
 
        // Pick the correct database for this operation 
        SQLiteDatabase db = getDatabase(context);
 
        if (ACTIVATE_ALL_LOGS) {
            Log.d(LOG_TAG, "bulkInsert: uri=" + uri + ", match is " + uriType.name());
        } 
 
        int numberInserted = 0;
        SQLiteStatement insertStmt;
 
        db.beginTransaction();
        try { 
            switch (uriType) {
                case DB_PERSON:
                    insertStmt = db.compileStatement(DbPerson.getBulkInsertString());
                    for (ContentValues value : values) {
                        DbPerson.bindValuesInBulkInsert(insertStmt, value);
                        insertStmt.execute();
                        insertStmt.clearBindings();
                    } 
                    insertStmt.close();
                    db.setTransactionSuccessful();
                    numberInserted = values.length;
 
			        if (ACTIVATE_ALL_LOGS) {
			            Log.d(LOG_TAG, "bulkInsert: uri=" + uri + " | nb inserts : " + numberInserted);
			        } 
                    break; 
 
                default: 
                    throw new IllegalArgumentException("Unknown URI " + uri);
            } 
        } finally { 
            db.endTransaction();
        } 
 
        // Notify with the base uri, not the new uri (nobody is watching a new 
        // record) 
        context.getContentResolver().notifyChange(uri, null);
        return numberInserted;
    } 
 
    @Override 
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 
            String sortOrder) {
 
        Cursor c = null;
        Uri notificationUri = PoCContent.CONTENT_URI;
        UriType uriType = matchUri(uri);
        Context context = getContext();
        // Pick the correct database for this operation 
        SQLiteDatabase db = getDatabase(context);
        String id;
 
        if (ACTIVATE_ALL_LOGS) {
            Log.d(LOG_TAG, "query: uri=" + uri + ", match is " + uriType.name());
        } 
 
        switch (uriType) {
            case DB_PERSON_ID:
                id = uri.getPathSegments().get(1);
                c = db.query(uriType.getTableName(), projection, whereWithId(id, selection),
                        selectionArgs, null, null, sortOrder);
                break; 
            case DB_PERSON:
                c = db.query(uriType.getTableName(), projection, selection, selectionArgs,
                        null, null, sortOrder);
                break; 
        } 
 
        if ((c != null) && !isTemporary()) {
            c.setNotificationUri(getContext().getContentResolver(), notificationUri);
        } 
        return c;
    } 
 
    private String whereWithId(String id, String selection) {
        StringBuilder sb = new StringBuilder(256);
        sb.append(BaseColumns._ID);
        sb.append(" = ");
        sb.append(id);
        if (selection != null) {
            sb.append(" AND (");
            sb.append(selection);
            sb.append(')');
        } 
        return sb.toString();
    } 
 
    @Override 
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
 
        UriType uriType = matchUri(uri);
        Context context = getContext();
 
        // Pick the correct database for this operation 
        SQLiteDatabase db = getDatabase(context);
 
        if (ACTIVATE_ALL_LOGS) {
            Log.d(LOG_TAG, "update: uri=" + uri + ", match is " + uriType.name());
        } 
 
        int result = -1;
 
        switch (uriType) {
            case DB_PERSON_ID:
                String id = uri.getPathSegments().get(1);
                result = db.update(uriType.getTableName(), values, whereWithId(id, selection),
                        selectionArgs);
                break; 
            case DB_PERSON:
                result = db.update(uriType.getTableName(), values, selection, selectionArgs);
                break; 
        } 
 
        getContext().getContentResolver().notifyChange(uri, null);
        return result;
Stop searching for code, let great code find you!  Add Codota to your java IDE