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:org.ttrssreader.controllers.DBHelper.java

/**
 * delete all rows from feeds table//from w  w  w. j  av a2s . c  o  m
 */
void deleteFeeds() {
    if (!isDBAvailable())
        return;

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    try {
        db.delete(TABLE_FEEDS, null, null);
    } finally {
        writeLock(false);
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

void deleteCategories(boolean withVirtualCategories) {
    if (!isDBAvailable())
        return;/*from   w w  w.  j  a  va2s  . com*/

    String wherePart = "";
    if (!withVirtualCategories)
        wherePart = "_id > 0";

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    try {
        db.delete(TABLE_CATEGORIES, wherePart, null);
    } finally {
        writeLock(false);
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

private void purgeLabels() {
    if (!isDBAvailable())
        return;//from w w w .  ja v a 2s  . c o  m

    // @formatter:off
    String idsArticles = "SELECT a2l.articleId FROM " + TABLE_ARTICLES2LABELS + " AS a2l LEFT OUTER JOIN "
            + TABLE_ARTICLES + " AS a" + " ON a2l.articleId = a._id WHERE a._id IS null";

    String idsFeeds = "SELECT a2l.labelId FROM " + TABLE_ARTICLES2LABELS + " AS a2l LEFT OUTER JOIN "
            + TABLE_FEEDS + " AS f" + " ON a2l.labelId = f._id WHERE f._id IS null";
    // @formatter:on

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    try {
        db.delete(TABLE_ARTICLES2LABELS, "articleId IN(" + idsArticles + ")", null);
        db.delete(TABLE_ARTICLES2LABELS, "labelId IN(" + idsFeeds + ")", null);
    } finally {
        writeLock(false);
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

private void removeLabel(int articleId, Label label) {
    if (!isDBAvailable())
        return;/*from   w  w  w .  j  a  v a2s.c o m*/

    if (label.id < -10) {
        String[] args = new String[] { articleId + "", label.id + "" };

        SQLiteDatabase db = getOpenHelper().getWritableDatabase();
        writeLock(true);
        try {
            db.delete(TABLE_ARTICLES2LABELS, "articleId=? AND labelId=?", args);
        } finally {
            writeLock(false);
        }
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

/**
 * delete remote files with given IDs/*from  w  ww  . ja v  a2  s  . c om*/
 *
 * @param idList set of remote file IDs, which should be deleted
 * @return the number of deleted rows
 */
private int deleteRemoteFiles(Set<Integer> idList) {
    if (!isDBAvailable())
        return 0;

    int deletedCount = 0;
    if (idList != null && !idList.isEmpty()) {
        SQLiteDatabase db = getOpenHelper().getWritableDatabase();
        writeLock(true);
        try {
            for (String ids : StringSupport.convertListToString(idList, 400)) {
                deletedCount += db.delete(TABLE_REMOTEFILES, "id IN (" + ids + ")", null);
            }
        } finally {
            writeLock(false);
        }
    }
    return deletedCount;
}

From source file:org.ttrssreader.controllers.DBHelper.java

/**
 * delete articles and all its resources (e.g. remote files, labels etc.)
 *
 * @param whereClause the optional WHERE clause to apply when deleting.
 *                    Passing null will delete all rows.
 * @param whereArgs   You may include ?s in the where clause, which
 *                    will be replaced by the values from whereArgs. The values
 *                    will be bound as Strings.
 * @return the number of rows affected if a whereClause is passed in, 0
 * otherwise. To remove all rows and get a count pass "1" as the
 * whereClause./*from w w  w. jav a2  s  . c o  m*/
 */
private int safelyDeleteArticles(String whereClause, String[] whereArgs) {
    int deletedCount = 0;

    Collection<RemoteFile> rfs = getRemoteFilesForArticles(whereClause, whereArgs, true);
    if (!rfs.isEmpty()) {
        Set<Integer> rfIds = new HashSet<>(rfs.size());
        for (RemoteFile rf : rfs) {
            rfIds.add(rf.id);
            Controller.getInstance().getImageCache().getCacheFile(rf.url).delete();
        }
        deleteRemoteFiles(rfIds);
    }

    // @formatter:off
    StringBuilder query = new StringBuilder();
    query.append(" articleId IN (").append("     SELECT _id").append("       FROM ").append(TABLE_ARTICLES)
            .append("       WHERE ").append(whereClause).append(" )");
    // @formatter:on

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    db.beginTransaction();
    try {
        // first, delete article referencies from linking table to preserve foreign key constraint on the next step
        db.delete(TABLE_REMOTEFILE2ARTICLE, query.toString(), whereArgs);

        // TODO Foreign-key constraint failed from purgeOrphanedArticles() and safelyDeleteArticles()
        deletedCount = db.delete(TABLE_ARTICLES, whereClause, whereArgs);
        purgeLabels();
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        writeLock(false);
    }

    return deletedCount;
}

From source file:org.frc836.database.DBSyncService.java

private void processEvents(JSONArray events) {
    try {/*from   ww  w .ja va2s .c om*/
        for (int i = 0; i < events.length(); i++) {
            JSONObject row = events.getJSONObject(i);
            Action action = Action.UPDATE;
            if (row.getInt(EVENT_LU_Entry.COLUMN_NAME_INVALID) != 0) {
                action = Action.DELETE;
            }
            ContentValues vals = new ContentValues();
            vals.put(EVENT_LU_Entry.COLUMN_NAME_ID, row.getInt(EVENT_LU_Entry.COLUMN_NAME_ID));
            vals.put(EVENT_LU_Entry.COLUMN_NAME_EVENT_NAME,
                    row.getString(EVENT_LU_Entry.COLUMN_NAME_EVENT_NAME));
            vals.put(EVENT_LU_Entry.COLUMN_NAME_EVENT_CODE,
                    row.getString(EVENT_LU_Entry.COLUMN_NAME_EVENT_CODE));
            vals.put(EVENT_LU_Entry.COLUMN_NAME_DATE_START,
                    row.getString(EVENT_LU_Entry.COLUMN_NAME_DATE_START));
            vals.put(EVENT_LU_Entry.COLUMN_NAME_TIMESTAMP,
                    DB.dateParser.format(new Date(row.getLong(EVENT_LU_Entry.COLUMN_NAME_TIMESTAMP) * 1000)));

            // check if this entry exists already
            String[] projection = { EVENT_LU_Entry.COLUMN_NAME_EVENT_NAME };
            String[] where = { vals.getAsString(EVENT_LU_Entry.COLUMN_NAME_ID) };
            synchronized (ScoutingDBHelper.lock) {

                SQLiteDatabase db = ScoutingDBHelper.getInstance().getWritableDatabase();

                Cursor c = db.query(EVENT_LU_Entry.TABLE_NAME, projection, // select
                        EVENT_LU_Entry.COLUMN_NAME_ID + "=?", where, null, // don't
                        // group
                        null, // don't filter
                        null, // don't order
                        "0,1"); // limit to 1
                try {
                    if (!c.moveToFirst()) {
                        if (action == Action.UPDATE)
                            action = Action.INSERT;
                        else if (action == Action.DELETE)
                            action = Action.NOTHING;
                    }

                    switch (action) {
                    case UPDATE:
                        db.update(EVENT_LU_Entry.TABLE_NAME, vals, EVENT_LU_Entry.COLUMN_NAME_ID + " = ?",
                                where);
                        break;
                    case INSERT:
                        db.insert(EVENT_LU_Entry.TABLE_NAME, null, vals);
                        break;
                    case DELETE:
                        db.delete(EVENT_LU_Entry.TABLE_NAME, EVENT_LU_Entry.COLUMN_NAME_ID + " = ?", where);
                        break;
                    default:
                    }
                } finally {
                    if (c != null)
                        c.close();
                    ScoutingDBHelper.getInstance().close();
                }
            }

        }
    } catch (JSONException e) {
        // TODO handle error
    }
}

From source file:org.frc836.database.DBSyncService.java

private void processRobots(JSONArray robots) {
    try {/*from   www  .  j ava  2 s.  co  m*/
        for (int i = 0; i < robots.length(); i++) {
            JSONObject row = robots.getJSONObject(i);
            Action action = Action.UPDATE;
            if (row.getInt(ROBOT_LU_Entry.COLUMN_NAME_INVALID) != 0) {
                action = Action.DELETE;
            }
            ContentValues vals = new ContentValues();
            vals.put(ROBOT_LU_Entry.COLUMN_NAME_ID, row.getInt(ROBOT_LU_Entry.COLUMN_NAME_ID));
            vals.put(ROBOT_LU_Entry.COLUMN_NAME_TEAM_ID, row.getString(ROBOT_LU_Entry.COLUMN_NAME_TEAM_ID));
            vals.put(ROBOT_LU_Entry.COLUMN_NAME_ROBOT_PHOTO,
                    row.getString(ROBOT_LU_Entry.COLUMN_NAME_ROBOT_PHOTO));
            vals.put(ROBOT_LU_Entry.COLUMN_NAME_TIMESTAMP,
                    DB.dateParser.format(new Date(row.getLong(ROBOT_LU_Entry.COLUMN_NAME_TIMESTAMP) * 1000)));

            // check if this entry exists already
            String[] projection = { ROBOT_LU_Entry.COLUMN_NAME_ID };
            String[] where = { vals.getAsString(ROBOT_LU_Entry.COLUMN_NAME_TEAM_ID) };
            synchronized (ScoutingDBHelper.lock) {

                SQLiteDatabase db = ScoutingDBHelper.getInstance().getWritableDatabase();

                Cursor c = db.query(ROBOT_LU_Entry.TABLE_NAME, projection, // select
                        ROBOT_LU_Entry.COLUMN_NAME_TEAM_ID + "=?", where, null, // don't
                        // group
                        null, // don't filter
                        null, // don't order
                        "0,1"); // limit to 1
                try {
                    if (!c.moveToFirst()) {
                        if (action == Action.UPDATE)
                            action = Action.INSERT;
                        else if (action == Action.DELETE)
                            action = Action.NOTHING;
                    }

                    switch (action) {
                    case UPDATE:
                        db.update(ROBOT_LU_Entry.TABLE_NAME, vals, ROBOT_LU_Entry.COLUMN_NAME_TEAM_ID + " = ?",
                                where);
                        break;
                    case INSERT:
                        db.insert(ROBOT_LU_Entry.TABLE_NAME, null, vals);
                        break;
                    case DELETE:
                        db.delete(ROBOT_LU_Entry.TABLE_NAME, ROBOT_LU_Entry.COLUMN_NAME_TEAM_ID + " = ?",
                                where);
                        break;
                    default:
                    }
                } finally {
                    if (c != null)
                        c.close();
                    ScoutingDBHelper.getInstance().close();
                }
            }
        }
    } catch (JSONException e) {
        // TODO handle error
    }
}

From source file:org.frc836.database.DBSyncService.java

private void processPositions(JSONArray positions) {
    try {//from www  .  j a  va2s  .  c o  m
        for (int i = 0; i < positions.length(); i++) {
            JSONObject row = positions.getJSONObject(i);
            Action action = Action.UPDATE;
            if (row.getInt(POSITION_LU_Entry.COLUMN_NAME_INVALID) != 0) {
                action = Action.DELETE;
            }
            ContentValues vals = new ContentValues();
            vals.put(POSITION_LU_Entry.COLUMN_NAME_ID, row.getInt(POSITION_LU_Entry.COLUMN_NAME_ID));
            vals.put(POSITION_LU_Entry.COLUMN_NAME_POSITION,
                    row.getString(POSITION_LU_Entry.COLUMN_NAME_POSITION));
            vals.put(POSITION_LU_Entry.COLUMN_NAME_TIMESTAMP, DB.dateParser
                    .format(new Date(row.getLong(POSITION_LU_Entry.COLUMN_NAME_TIMESTAMP) * 1000)));

            // check if this entry exists already
            String[] projection = { POSITION_LU_Entry.COLUMN_NAME_POSITION };
            String[] where = { vals.getAsString(POSITION_LU_Entry.COLUMN_NAME_ID) };

            synchronized (ScoutingDBHelper.lock) {

                SQLiteDatabase db = ScoutingDBHelper.getInstance().getWritableDatabase();

                Cursor c = db.query(POSITION_LU_Entry.TABLE_NAME, projection, // select
                        POSITION_LU_Entry.COLUMN_NAME_ID + "=?", where, null, // don't
                        // group
                        null, // don't filter
                        null, // don't order
                        "0,1"); // limit to 1
                try {
                    if (!c.moveToFirst()) {
                        if (action == Action.UPDATE)
                            action = Action.INSERT;
                        else if (action == Action.DELETE)
                            action = Action.NOTHING;
                    }

                    switch (action) {
                    case UPDATE:
                        db.update(POSITION_LU_Entry.TABLE_NAME, vals, POSITION_LU_Entry.COLUMN_NAME_ID + " = ?",
                                where);
                        break;
                    case INSERT:
                        db.insert(POSITION_LU_Entry.TABLE_NAME, null, vals);
                        break;
                    case DELETE:
                        db.delete(POSITION_LU_Entry.TABLE_NAME, POSITION_LU_Entry.COLUMN_NAME_ID + " = ?",
                                where);
                        break;
                    default:
                    }
                } finally {
                    if (c != null)
                        c.close();
                    ScoutingDBHelper.getInstance().close();
                }
            }
        }
    } catch (JSONException e) {
        // TODO handle error
    }
}

From source file:org.frc836.database.DBSyncService.java

private void processNotes(JSONArray notes) {
    try {//from   ww w  .  j  ava  2s  .  c o m
        for (int i = 0; i < notes.length(); i++) {
            JSONObject row = notes.getJSONObject(i);
            Action action = Action.UPDATE;
            if (row.getInt(NOTES_OPTIONS_Entry.COLUMN_NAME_INVALID) != 0) {
                action = Action.DELETE;
            }
            ContentValues vals = new ContentValues();
            vals.put(NOTES_OPTIONS_Entry.COLUMN_NAME_ID, row.getInt(NOTES_OPTIONS_Entry.COLUMN_NAME_ID));
            vals.put(NOTES_OPTIONS_Entry.COLUMN_NAME_OPTION_TEXT,
                    row.getString(NOTES_OPTIONS_Entry.COLUMN_NAME_OPTION_TEXT));
            vals.put(NOTES_OPTIONS_Entry.COLUMN_NAME_TIMESTAMP, DB.dateParser
                    .format(new Date(row.getLong(NOTES_OPTIONS_Entry.COLUMN_NAME_TIMESTAMP) * 1000)));

            // check if this entry exists already
            String[] projection = { NOTES_OPTIONS_Entry.COLUMN_NAME_OPTION_TEXT };
            String[] where = { vals.getAsString(NOTES_OPTIONS_Entry.COLUMN_NAME_ID) };

            synchronized (ScoutingDBHelper.lock) {

                SQLiteDatabase db = ScoutingDBHelper.getInstance().getWritableDatabase();

                Cursor c = db.query(NOTES_OPTIONS_Entry.TABLE_NAME, projection, // select
                        NOTES_OPTIONS_Entry.COLUMN_NAME_ID + "=?", where, null, // don't
                        // group
                        null, // don't filter
                        null, // don't order
                        "0,1"); // limit to 1
                try {
                    if (!c.moveToFirst()) {
                        if (action == Action.UPDATE)
                            action = Action.INSERT;
                        else if (action == Action.DELETE)
                            action = Action.NOTHING;
                    }

                    switch (action) {
                    case UPDATE:
                        db.update(NOTES_OPTIONS_Entry.TABLE_NAME, vals,
                                NOTES_OPTIONS_Entry.COLUMN_NAME_ID + " = ?", where);
                        break;
                    case INSERT:
                        db.insert(NOTES_OPTIONS_Entry.TABLE_NAME, null, vals);
                        break;
                    case DELETE:
                        db.delete(NOTES_OPTIONS_Entry.TABLE_NAME, NOTES_OPTIONS_Entry.COLUMN_NAME_ID + " = ?",
                                where);
                        break;
                    default:
                    }
                } finally {
                    if (c != null)
                        c.close();
                    ScoutingDBHelper.getInstance().close();
                }
            }
        }
    } catch (JSONException e) {
        // TODO handle error
    }
}