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:com.csipsimple.db.DBProvider.java

@Override
public Uri insert(Uri uri, ContentValues initialValues) {
    int matched = URI_MATCHER.match(uri);
    String matchedTable = null;//from   w  w w  .jav  a 2s .c  o  m
    Uri baseInsertedUri = null;

    switch (matched) {
    case ACCOUNTS:
    case ACCOUNTS_ID:
        matchedTable = SipProfile.ACCOUNTS_TABLE_NAME;
        baseInsertedUri = SipProfile.ACCOUNT_ID_URI_BASE;
        break;
    case CALLLOGS:
    case CALLLOGS_ID:
        matchedTable = SipManager.CALLLOGS_TABLE_NAME;
        baseInsertedUri = SipManager.CALLLOG_ID_URI_BASE;
        break;
    case FILTERS:
    case FILTERS_ID:
        matchedTable = SipManager.FILTERS_TABLE_NAME;
        baseInsertedUri = SipManager.FILTER_ID_URI_BASE;
        break;
    case MESSAGES:
    case MESSAGES_ID:
        matchedTable = SipMessage.MESSAGES_TABLE_NAME;
        baseInsertedUri = SipMessage.MESSAGE_ID_URI_BASE;
        break;
    case ACCOUNTS_STATUS_ID:
        long id = ContentUris.parseId(uri);
        synchronized (profilesStatus) {
            SipProfileState ps = new SipProfileState();
            if (profilesStatus.containsKey(id)) {
                ContentValues currentValues = profilesStatus.get(id);
                ps.createFromContentValue(currentValues);
            }
            ps.createFromContentValue(initialValues);
            ContentValues cv = ps.getAsContentValue();
            cv.put(SipProfileState.ACCOUNT_ID, id);
            profilesStatus.put(id, cv);
            Log.d(THIS_FILE, "Added " + cv);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return uri;
    default:
        break;
    }

    if (matchedTable == null) {
        throw new IllegalArgumentException(UNKNOWN_URI_LOG + uri);
    }

    ContentValues values;

    if (initialValues != null) {
        values = new ContentValues(initialValues);
    } else {
        values = new ContentValues();
    }

    SQLiteDatabase db = mOpenHelper.getWritableDatabase();

    long rowId = db.insert(matchedTable, null, values);

    // If the insert succeeded, the row ID exists.
    if (rowId >= 0) {
        // TODO : for inserted account register it here

        Uri retUri = ContentUris.withAppendedId(baseInsertedUri, rowId);
        getContext().getContentResolver().notifyChange(retUri, null);

        if (matched == ACCOUNTS || matched == ACCOUNTS_ID) {
            broadcastAccountChange(rowId);
        }
        if (matched == CALLLOGS || matched == CALLLOGS_ID) {
            db.delete(SipManager.CALLLOGS_TABLE_NAME,
                    CallLog.Calls._ID + " IN " + "(SELECT " + CallLog.Calls._ID + " FROM "
                            + SipManager.CALLLOGS_TABLE_NAME + " ORDER BY " + CallLog.Calls.DEFAULT_SORT_ORDER
                            + " LIMIT -1 OFFSET 500)",
                    null);
        }
        if (matched == ACCOUNTS_STATUS || matched == ACCOUNTS_STATUS_ID) {
            broadcastRegistrationChange(rowId);
        }
        if (matched == FILTERS || matched == FILTERS_ID) {
            Filter.resetCache();
        }

        return retUri;
    }

    throw new SQLException("Failed to insert row into " + uri);
}

From source file:net.potterpcs.recipebook.RecipeData.java

public int updateRecipe(Recipe r) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int ret = -1;
        try {/* w  ww  .  java  2s . c o m*/
            long rid = r.id;
            String[] whereArgs = { Long.toString(rid) };
            ret = db.update(RECIPES_TABLE, createRecipeForInsert(r), RT_ID + " = ?",
                    new String[] { Long.toString(r.id) });

            // TODO until we can figure out a smarter way to update
            db.delete(INGREDIENTS_TABLE, IT_RECIPE_ID + " = ?", whereArgs);
            for (String ing : r.ingredients) {
                db.insertWithOnConflict(INGREDIENTS_TABLE, null, createIngredientsCV(rid, ing),
                        SQLiteDatabase.CONFLICT_IGNORE);
            }

            db.delete(DIRECTIONS_TABLE, DT_RECIPE_ID + " = ?", whereArgs);
            int step = 1;
            for (String dir : r.directions) {
                db.insertWithOnConflict(DIRECTIONS_TABLE, null,
                        createDirectionsCV(rid, step, dir, r.directions_photos[step - 1]),
                        SQLiteDatabase.CONFLICT_IGNORE);
                step++;
            }

            db.delete(TAGS_TABLE, TT_RECIPE_ID + " = ?", whereArgs);
            for (String tag : r.tags) {
                db.insertWithOnConflict(TAGS_TABLE, null, createTagsCV(rid, tag),
                        SQLiteDatabase.CONFLICT_IGNORE);
            }
        } finally {
            db.close();
        }
        return ret;
    }
}

From source file:com.openerp.orm.ORM.java

public boolean delete(BaseDBHelper db, int id, boolean fromLocal) {
    try {//ww  w  .j av  a2s  .c  o m
        if (!fromLocal) {

            if (oe_obj.unlink(db.getModelName(), id)) {
                SQLiteDatabase sdb = getWritableDatabase();
                String where = "id = " + id;
                sdb.delete(modelToTable(db.getModelName()), where, null);
                sdb.close();
                return true;
            }
        } else {
            SQLiteDatabase sdb = getWritableDatabase();
            String where = "id = " + id;
            sdb.delete(modelToTable(db.getModelName()), where, null);
            sdb.close();
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:edu.cens.loci.provider.LociDbUtils.java

public int delete(SQLiteDatabase db, Cursor c) {
    long dataId = c.getLong(DataDeleteQuery._ID);
    //long placeId = c.getLong(DataDeleteQuery.PLACE_ID);
    mSelectionArgs1[0] = String.valueOf(dataId);
    int count = db.delete(Tables.DATA, Data._ID + "=?", mSelectionArgs1);
    return count;
}

From source file:org.totschnig.myexpenses.provider.TransactionDatabase.java

@Override
public void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
    //since API 16 we could use onConfigure to enable foreign keys
    //which is run before onUpgrade
    //but this makes upgrades more difficult, since then you have to maintain the constraint in
    //each step of a multi statement upgrade with table rename
    //we stick to doing upgrades with foreign keys disabled which forces us
    //to take care of ensuring consistency during upgrades
    if (!db.isReadOnly()) {
        db.execSQL("PRAGMA foreign_keys=ON;");
    }/* w ww.j  a  v a2 s .  c  o m*/
    try {
        db.delete(TABLE_TRANSACTIONS, KEY_STATUS + " = " + STATUS_UNCOMMITTED, null);
    } catch (SQLiteException e) {
        AcraHelper.report(e, DbUtils.getTableDetails(db.query("sqlite_master", new String[] { "name", "sql" },
                "type = 'table'", null, null, null, null)));
    }
}

From source file:me.piebridge.bible.Bible.java

public boolean deleteNote(Note note) {
    if (note == null || note.getId() == null) {
        return false;
    }/*from   w  w w .j a  v  a  2s.c o  m*/
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    if (!isDatabaseIntegrityOk(db)) {
        return true;
    }
    db.delete(AnnotationsDatabaseHelper.TABLE_ANNOTATIONS, AnnotationsDatabaseHelper.COLUMN_ID + " = ?",
            new String[] { String.valueOf(note.getId()) });
    notes.remove(note.verse);
    return true;
}

From source file:ru.orangesoftware.financisto2.db.DatabaseAdapter.java

public void addAttributes(long categoryId, List<Attribute> attributes) {
    SQLiteDatabase db = db();
    db.delete(CATEGORY_ATTRIBUTE_TABLE, CategoryAttributeColumns.CATEGORY_ID + "=?",
            new String[] { String.valueOf(categoryId) });
    if (attributes != null && attributes.size() > 0) {
        ContentValues values = new ContentValues();
        values.put(CategoryAttributeColumns.CATEGORY_ID, categoryId);
        for (Attribute a : attributes) {
            values.put(CategoryAttributeColumns.ATTRIBUTE_ID, a.id);
            db.insert(CATEGORY_ATTRIBUTE_TABLE, null, values);
        }//from  www  . j  av a2 s . c  o m
    }
}

From source file:ru.orangesoftware.financisto2.db.DatabaseAdapter.java

public void deleteTransactionNoDbTransaction(long id) {
    Transaction t = getTransaction(id);//from  w w  w  . j a  va 2s  .c o  m
    if (t.isNotTemplateLike()) {
        revertFromAccountBalance(t);
        revertToAccountBalance(t);
        updateAccountLastTransactionDate(t.fromAccountId);
        updateAccountLastTransactionDate(t.toAccountId);
    }
    String[] sid = new String[] { String.valueOf(id) };
    SQLiteDatabase db = db();
    db.delete(TRANSACTION_ATTRIBUTE_TABLE, TransactionAttributeColumns.TRANSACTION_ID + "=?", sid);
    db.delete(TRANSACTION_TABLE, TransactionColumns._id + "=?", sid);
    writeDeleteLog(TRANSACTION_TABLE, t.remoteKey);
    deleteSplitsForParentTransaction(id);
}

From source file:com.nadmm.airports.DownloadActivity.java

protected void cleanupExpiredData() {
    SQLiteDatabase catalogDb = mDbManager.getCatalogDb();

    Time now = new Time();
    now.setToNow();/*from  w  ww  . jav a2s . c  om*/
    String today = now.format3339(true);

    Cursor c = mDbManager.getAllFromCatalog();
    if (c.moveToFirst()) {
        do {
            // Check and delete all the expired databases
            String end = c.getString(c.getColumnIndex(Catalog.END_DATE));
            if (end.compareTo(today) < 0) {
                // This database has expired, remove it
                Integer _id = c.getInt(c.getColumnIndex(Catalog._ID));
                String dbName = c.getString(c.getColumnIndex(Catalog.DB_NAME));
                File file = new File(DatabaseManager.DATABASE_DIR, dbName);
                if (catalogDb.isOpen() && file.delete()) {
                    // Now delete the catalog entry for the file
                    catalogDb.delete(Catalog.TABLE_NAME, Catalog._ID + "=?",
                            new String[] { Integer.toString(_id) });
                }
            }
        } while (c.moveToNext());
    }
    c.close();
}

From source file:ru.orangesoftware.financisto2.db.DatabaseAdapter.java

private void deleteRateInTransaction(long fromCurrencyId, long toCurrencyId, long date, SQLiteDatabase db) {
    long d = DateUtils.atMidnight(date);
    db.delete(EXCHANGE_RATES_TABLE, ExchangeRateColumns.DELETE_CLAUSE,
            new String[] { String.valueOf(fromCurrencyId), String.valueOf(toCurrencyId), String.valueOf(d) });
    db.delete(EXCHANGE_RATES_TABLE, ExchangeRateColumns.DELETE_CLAUSE,
            new String[] { String.valueOf(toCurrencyId), String.valueOf(fromCurrencyId), String.valueOf(d) });
}