Example usage for android.database.sqlite SQLiteDatabase delete

List of usage examples for android.database.sqlite SQLiteDatabase delete

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase delete.

Prototype

public int delete(String table, String whereClause, String[] whereArgs) 

Source Link

Document

Convenience method for deleting rows in the database.

Usage

From source file:ru.gkpromtech.exhibition.db.Table.java

public void deleteById(List<Integer> ids) {
    if (ids.isEmpty())
        return;/*from ww w  .ja  va 2 s .  co  m*/

    SQLiteDatabase db = mSqlHelper.getWritableDatabase();
    try {
        db.beginTransaction();

        String args[] = DbHelper.makeArguments(ids.toArray(new Integer[ids.size()]));
        String params = DbHelper.makePlaceholders(args.length);
        db.delete(mTableName, "id IN (" + params + ")", args);

        db.setTransactionSuccessful();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.endTransaction();
        db.close();
    }
}

From source file:org.thomnichols.android.gmarks.GmarksProvider.java

@Override
public int delete(Uri uri, String where, String[] whereArgs) {
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    int count;//from  ww w.j ava  2  s  . c  o  m
    switch (sUriMatcher.match(uri)) {
    case BOOKMARKS_URI:
        count = db.delete(BOOKMARKS_TABLE_NAME, where, whereArgs);
        break;

    case BOOKMARK_ID_URI:
        String noteId = uri.getPathSegments().get(1);
        count = db.delete(BOOKMARKS_TABLE_NAME,
                Bookmark.Columns._ID + "=" + noteId + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""),
                whereArgs);
        break;
    // TODO delete item from text search!

    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    getContext().getContentResolver().notifyChange(uri, null);
    return count;
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public int deleteResponseFilter(long id) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_RESPONSE_FILTERS, KEY_ID + " = ?", new String[] { String.valueOf(id) });
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public int deleteResponseFilterFile(String source) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_RESPONSE_FILTERS, KEY_SOURCE + " = ?", new String[] { source });
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public int deleteDomainFilter(long id) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_DOMAIN_FILTERS, KEY_ID + " = ?", new String[] { String.valueOf(id) });
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public int deleteDomainFilterFile(String source) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_DOMAIN_FILTERS, KEY_SOURCE + " = ?", new String[] { source });
}

From source file:cn.edu.wyu.documentviewer.RecentsProvider.java

/**
 * Purge all internal data whose authority matches the given
 * {@link Predicate}.//from   w  ww.  j a v a 2s. co m
 */
private void purgeByAuthority(Predicate<String> predicate) {
    final SQLiteDatabase db = mHelper.getWritableDatabase();
    final DocumentStack stack = new DocumentStack();

    Cursor cursor = db.query(TABLE_RECENT, null, null, null, null, null, null);
    try {
        while (cursor.moveToNext()) {
            try {
                final byte[] rawStack = cursor.getBlob(cursor.getColumnIndex(RecentColumns.STACK));
                DurableUtils.readFromArray(rawStack, stack);

                if (stack.root != null && predicate.apply(stack.root.authority)) {
                    final String key = getCursorString(cursor, RecentColumns.KEY);
                    db.delete(TABLE_RECENT, RecentColumns.KEY + "=?", new String[] { key });
                }
            } catch (IOException ignored) {
            }
        }
    } finally {
        //IoUtils.closeQuietly(cursor);
        IOUtils.closeQuietly(cursor);
    }

    cursor = db.query(TABLE_STATE, new String[] { StateColumns.AUTHORITY }, null, null, StateColumns.AUTHORITY,
            null, null);
    try {
        while (cursor.moveToNext()) {
            final String authority = getCursorString(cursor, StateColumns.AUTHORITY);
            if (predicate.apply(authority)) {
                db.delete(TABLE_STATE, StateColumns.AUTHORITY + "=?", new String[] { authority });
                Log.d(TAG, "Purged state for " + authority);
            }
        }
    } finally {
        //IoUtils.closeQuietly(cursor);
        IOUtils.closeQuietly(cursor);
    }

    cursor = db.query(TABLE_RESUME, null, null, null, null, null, null);
    try {
        while (cursor.moveToNext()) {
            try {
                final byte[] rawStack = cursor.getBlob(cursor.getColumnIndex(ResumeColumns.STACK));
                DurableUtils.readFromArray(rawStack, stack);

                if (stack.root != null && predicate.apply(stack.root.authority)) {
                    final String packageName = getCursorString(cursor, ResumeColumns.PACKAGE_NAME);
                    db.delete(TABLE_RESUME, ResumeColumns.PACKAGE_NAME + "=?", new String[] { packageName });
                }
            } catch (IOException ignored) {
            }
        }
    } finally {
        //IoUtils.closeQuietly(cursor);
        IOUtils.closeQuietly(cursor);
    }
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public boolean removePendingNotification(int notificationId) {
    SQLiteDatabase db = this.getWritableDatabase();
    int id = (int) db.delete(TABLE_PENDING_NOTIFICATIONS, KEY_NOTIFICATION_ID + "='" + notificationId + "'",
            null);/* w  w  w .  j a  va 2 s.c  om*/
    return id >= 0;
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public boolean removeTrustedAccessPoint(TrustedAccessPoint tap) {
    SQLiteDatabase db = this.getWritableDatabase();
    int id = (int) db.delete(TABLE_TRUSTED_ACCESS_POINTS,
            KEY_SSID + "='" + tap.ssid + "' AND " + KEY_BSSID + "='" + tap.bssid + "'", null);
    return id > 0;
}

From source file:org.opendatakit.common.android.provider.impl.FormsProviderImpl.java

/**
 * This method removes the entry from the content provider, and also removes
 * any associated files. files: form.xml, [formmd5].formdef, formname
 * {directory}/*from   w  w w .  ja  v  a 2  s.  c om*/
 */
@Override
public int delete(Uri uri, String where, String[] whereArgs) {
    List<String> segments = uri.getPathSegments();

    if (segments.size() < 1 || segments.size() > 2) {
        throw new IllegalArgumentException("Unknown URI (incorrect number of segments!) " + uri);
    }

    String appName = segments.get(0);
    ODKFileUtils.verifyExternalStorageAvailability();
    ODKFileUtils.assertDirectoryStructure(appName);
    WebLogger log = WebLogger.getLogger(appName);

    String uriFormId = ((segments.size() == 2) ? segments.get(1) : null);
    boolean isNumericId = StringUtils.isNumeric(uriFormId);

    // Modify the where clause to account for the presence of
    // a form id. Accept either:
    // (1) numeric _ID value
    // (2) string FORM_ID value.
    String whereId;
    String[] whereIdArgs;

    if (uriFormId == null) {
        whereId = where;
        whereIdArgs = whereArgs;
    } else {
        if (TextUtils.isEmpty(where)) {
            whereId = (isNumericId ? FormsColumns._ID : FormsColumns.FORM_ID) + "=?";
            whereIdArgs = new String[1];
            whereIdArgs[0] = uriFormId;
        } else {
            whereId = (isNumericId ? FormsColumns._ID : FormsColumns.FORM_ID) + "=? AND (" + where + ")";
            whereIdArgs = new String[whereArgs.length + 1];
            whereIdArgs[0] = uriFormId;
            for (int i = 0; i < whereArgs.length; ++i) {
                whereIdArgs[i + 1] = whereArgs[i];
            }
        }
    }

    Cursor del = null;
    Integer idValue = null;
    String tableIdValue = null;
    String formIdValue = null;
    HashMap<File, DirType> mediaDirs = new HashMap<File, DirType>();
    try {
        del = this.query(uri, null, whereId, whereIdArgs, null);
        if (del == null) {
            throw new SQLException("FAILED Delete into " + uri + " -- unable to query for existing records");
        }
        del.moveToPosition(-1);
        while (del.moveToNext()) {
            idValue = ODKDatabaseUtils.get().getIndexAsType(del, Integer.class,
                    del.getColumnIndex(FormsColumns._ID));
            tableIdValue = ODKDatabaseUtils.get().getIndexAsString(del,
                    del.getColumnIndex(FormsColumns.TABLE_ID));
            formIdValue = ODKDatabaseUtils.get().getIndexAsString(del,
                    del.getColumnIndex(FormsColumns.FORM_ID));
            File mediaDir = ODKFileUtils.asAppFile(appName, ODKDatabaseUtils.get().getIndexAsString(del,
                    del.getColumnIndex(FormsColumns.APP_RELATIVE_FORM_MEDIA_PATH)));
            mediaDirs.put(mediaDir, (tableIdValue == null) ? DirType.FRAMEWORK : DirType.FORMS);
        }
    } catch (Exception e) {
        log.w(t, "FAILED Delete from " + uri + " -- query for existing row failed: " + e.toString());

        if (e instanceof SQLException) {
            throw (SQLException) e;
        } else {
            throw new SQLException(
                    "FAILED Delete from " + uri + " -- query for existing row failed: " + e.toString());
        }
    } finally {
        if (del != null && !del.isClosed()) {
            del.close();
        }
    }

    SQLiteDatabase db = null;
    int count;
    try {
        db = DatabaseFactory.get().getDatabase(getContext(), appName);
        db.beginTransaction();
        count = db.delete(DatabaseConstants.FORMS_TABLE_NAME, whereId, whereIdArgs);
        db.setTransactionSuccessful();
    } catch (Exception e) {
        e.printStackTrace();
        log.w(t, "Unable to perform deletion " + e.toString());
        return 0;
    } finally {
        if (db != null) {
            db.endTransaction();
            db.close();
        }
    }

    // and attempt to move these directories to the stale forms location
    // so that they do not immediately get rescanned...

    for (HashMap.Entry<File, DirType> entry : mediaDirs.entrySet()) {
        try {
            moveDirectory(appName, entry.getValue(), entry.getKey());
        } catch (IOException e) {
            e.printStackTrace();
            log.e(t, "Unable to move directory " + e.toString());
        }
    }

    if (count == 1) {
        Uri formUri = Uri.withAppendedPath(
                Uri.withAppendedPath(Uri.parse("content://" + getFormsAuthority()), appName), formIdValue);
        getContext().getContentResolver().notifyChange(formUri, null);
        Uri idUri = Uri.withAppendedPath(
                Uri.withAppendedPath(Uri.parse("content://" + getFormsAuthority()), appName),
                Long.toString(idValue));
        getContext().getContentResolver().notifyChange(idUri, null);
    } else {
        getContext().getContentResolver().notifyChange(uri, null);
    }
    return count;
}