Example usage for android.database.sqlite SQLiteStatement executeUpdateDelete

List of usage examples for android.database.sqlite SQLiteStatement executeUpdateDelete

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteStatement executeUpdateDelete.

Prototype

public int executeUpdateDelete() 

Source Link

Document

Execute this SQL statement, if the number of rows affected by execution of this SQL statement is of any importance to the caller - for example, UPDATE / DELETE SQL statements.

Usage

From source file:com.nolanlawson.cordova.sqlite.SQLitePlugin.java

private SQLitePLuginResult doUpdateInBackgroundAndPossiblyThrow(String sql, String[] bindArgs,
        SQLiteDatabase db) {//from   w ww  . jav  a  2 s . c  om
    debug("\"run\" query: %s", sql);
    SQLiteStatement statement = null;
    try {
        statement = db.compileStatement(sql);
        debug("compiled statement");
        if (bindArgs != null) {
            statement.bindAllArgsAsStrings(bindArgs);
        }
        debug("bound args");
        if (isInsert(sql)) {
            debug("type: insert");
            long insertId = statement.executeInsert();
            int rowsAffected = insertId >= 0 ? 1 : 0;
            return new SQLitePLuginResult(EMPTY_ROWS, EMPTY_COLUMNS, rowsAffected, insertId, null);
        } else if (isDelete(sql) || isUpdate(sql)) {
            debug("type: update/delete");
            int rowsAffected = statement.executeUpdateDelete();
            return new SQLitePLuginResult(EMPTY_ROWS, EMPTY_COLUMNS, rowsAffected, 0, null);
        } else {
            // in this case, we don't need rowsAffected or insertId, so we can have a slight
            // perf boost by just executing the query
            debug("type: drop/create/etc.");
            statement.execute();
            return EMPTY_RESULT;
        }
    } finally {
        if (statement != null) {
            statement.close();
        }
    }
}

From source file:org.kontalk.provider.UsersProvider.java

private int executeUpdateDelete(SQLiteDatabase db, SQLiteStatement stm) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        return stm.executeUpdateDelete();
    } else {//from  ww w  . j  a va  2  s . c  om
        stm.execute();
        SQLiteStatement changes = db.compileStatement("SELECT changes()");
        try {
            return (int) changes.simpleQueryForLong();
        } finally {
            changes.close();
        }
    }
}

From source file:io.requery.android.database.sqlite.SQLiteDatabase.java

private int executeSql(String sql, Object[] bindArgs) throws SQLException {
    acquireReference();/*www. jav  a  2s  . co m*/
    try {
        SQLiteStatement statement = new SQLiteStatement(this, sql, bindArgs);
        try {
            return statement.executeUpdateDelete();
        } finally {
            statement.close();
        }
    } finally {
        releaseReference();
    }
}

From source file:io.requery.android.database.sqlite.SQLiteDatabase.java

/**
 * Convenience method for updating rows in the database.
 *
 * @param table the table to update in// w ww  . ja  v  a  2 s.  c om
 * @param values a map from column names to new column values. null is a
 *            valid value that will be translated to NULL.
 * @param whereClause the optional WHERE clause to apply when updating.
 *            Passing null will update 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.
 * @param conflictAlgorithm for update conflict resolver
 * @return the number of rows affected
 */
public int updateWithOnConflict(String table, ContentValues values, String whereClause, String[] whereArgs,
        @ConflictAlgorithm int conflictAlgorithm) {
    if (values == null || values.size() == 0) {
        throw new IllegalArgumentException("Empty values");
    }

    acquireReference();
    try {
        StringBuilder sql = new StringBuilder(120);
        sql.append("UPDATE ");
        sql.append(CONFLICT_VALUES[conflictAlgorithm]);
        sql.append(table);
        sql.append(" SET ");

        // move all bind args to one array
        int setValuesSize = values.size();
        int bindArgsSize = (whereArgs == null) ? setValuesSize : (setValuesSize + whereArgs.length);
        Object[] bindArgs = new Object[bindArgsSize];
        int i = 0;
        for (Map.Entry<String, Object> entry : values.valueSet()) {
            sql.append((i > 0) ? "," : "");
            sql.append(entry.getKey());
            bindArgs[i++] = entry.getValue();
            sql.append("=?");
        }
        if (whereArgs != null) {
            for (i = setValuesSize; i < bindArgsSize; i++) {
                bindArgs[i] = whereArgs[i - setValuesSize];
            }
        }
        if (!TextUtils.isEmpty(whereClause)) {
            sql.append(" WHERE ");
            sql.append(whereClause);
        }

        SQLiteStatement statement = new SQLiteStatement(this, sql.toString(), bindArgs);
        try {
            return statement.executeUpdateDelete();
        } finally {
            statement.close();
        }
    } finally {
        releaseReference();
    }
}

From source file:io.requery.android.database.sqlite.SQLiteDatabase.java

/**
 * Convenience method for deleting rows in the database.
 *
 * @param table the table to delete from
 * @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.//from www. j  a va 2s . c om
 * @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.
 */
public int delete(String table, String whereClause, String[] whereArgs) {
    acquireReference();
    try {
        SQLiteStatement statement = new SQLiteStatement(this,
                "DELETE FROM " + table + (!TextUtils.isEmpty(whereClause) ? " WHERE " + whereClause : ""),
                whereArgs);
        try {
            return statement.executeUpdateDelete();
        } finally {
            statement.close();
        }
    } finally {
        releaseReference();
    }
}