Java Code Examples for androidx.sqlite.db.SupportSQLiteStatement#close()

The following examples show how to use androidx.sqlite.db.SupportSQLiteStatement#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Database.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@SuppressWarnings("ThrowFromFinallyBlock")
@Override
public int delete(String table, String whereClause, Object[] whereArgs) {
  String query = "DELETE FROM " + table
    + (TextUtils.isEmpty(whereClause) ? "" : " WHERE " + whereClause);
  SupportSQLiteStatement statement = compileStatement(query);

  try {
    SimpleSQLiteQuery.bind(statement, whereArgs);
    return statement.executeUpdateDelete();
  }
  finally {
    try {
      statement.close();
    }
    catch (Exception e) {
      throw new RuntimeException("Exception attempting to close statement", e);
    }
  }
}
 
Example 2
Source File: Database.java    From kripton with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int delete(String table, String whereClause, Object[] whereArgs) {
	String query = "DELETE FROM " + table + (TextUtils.isEmpty(whereClause) ? "" : " WHERE " + whereClause);
	SupportSQLiteStatement statement = compileStatement(query);

	try {
		SimpleSQLiteQuery.bind(statement, whereArgs);
		return statement.executeUpdateDelete();
	} finally {
		try {
			statement.close();
		} catch (Exception e) {
			throw new RuntimeException("Exception attempting to close statement", e);
		}
	}
}
 
Example 3
Source File: KriptonDatabaseHelper.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Insert.
 *
 * @param context the context
 * @param sql the sql
 * @param contentValues the content values
 * @return the long
 */
public static long insert(SQLContext context, String sql, KriptonContentValues contentValues) {
	SupportSQLiteStatement ps = context.getDatabase().compileStatement(sql);
	try {
		contentValues.bind(ps);
		return ps.executeInsert();
	} finally {
		try {
			ps.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
 
Example 4
Source File: KriptonDatabaseHelper.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Update delete.
 *
 * @param context the context
 * @param sql the sql
 * @param contentValues the content values
 * @return the int
 */
public static int updateDelete(SQLContext context, String sql, KriptonContentValues contentValues) {
	SupportSQLiteStatement ps = context.getDatabase().compileStatement(sql);
	try {
		contentValues.bind(ps);
		return ps.executeUpdateDelete();
	} finally {
		try {
			ps.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
 
Example 5
Source File: Database.java    From cwac-saferoom with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@SuppressWarnings("ThrowFromFinallyBlock")
@Override
public int update(String table, int conflictAlgorithm, ContentValues values,
                  String whereClause, Object[] whereArgs) {
  // taken from SQLiteDatabase class.
  if (values == null || values.size() == 0) {
    throw new IllegalArgumentException("Empty values");
  }
  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 (String colName : values.keySet()) {
    sql.append((i > 0) ? "," : "");
    sql.append(colName);
    bindArgs[i++] = values.get(colName);
    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);
  }
  SupportSQLiteStatement statement = compileStatement(sql.toString());

  try {
    SimpleSQLiteQuery.bind(statement, bindArgs);
    return statement.executeUpdateDelete();
  }
  finally {
    try {
      statement.close();
    }
    catch (Exception e) {
      throw new RuntimeException("Exception attempting to close statement", e);
    }
  }
}
 
Example 6
Source File: Database.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int update(String table, int conflictAlgorithm, ContentValues values, String whereClause,
		Object[] whereArgs) {
	// taken from SQLiteDatabase class.
	if (values == null || values.size() == 0) {
		throw new IllegalArgumentException("Empty values");
	}
	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 (String colName : values.keySet()) {
		sql.append((i > 0) ? "," : "");
		sql.append(colName);
		bindArgs[i++] = values.get(colName);
		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);
	}
	SupportSQLiteStatement statement = compileStatement(sql.toString());

	try {
		SimpleSQLiteQuery.bind(statement, bindArgs);
		return statement.executeUpdateDelete();
	} finally {
		try {
			statement.close();
		} catch (Exception e) {
			throw new RuntimeException("Exception attempting to close statement", e);
		}
	}
}