Example usage for android.database DatabaseUtils sqlEscapeString

List of usage examples for android.database DatabaseUtils sqlEscapeString

Introduction

In this page you can find the example usage for android.database DatabaseUtils sqlEscapeString.

Prototype

public static String sqlEscapeString(String value) 

Source Link

Document

SQL-escape a string.

Usage

From source file:org.exfio.weavedroid.resource.LocalAddressBook.java

public void deleteAllExceptUIDs(String[] ids) {
    String where;/*from w  w  w  .  j  av  a  2s  . c o  m*/

    if (ids.length != 0) {
        List<String> sqlFileNames = new LinkedList<String>();
        for (String id : ids)
            sqlFileNames.add(DatabaseUtils.sqlEscapeString(id));
        where = entryColumnUID() + " NOT IN (" + StringUtils.join(sqlFileNames, ",") + ")";
    } else
        where = entryColumnUID() + " IS NOT NULL";

    Builder builder = ContentProviderOperation.newDelete(entriesURI()).withSelection(where, null);
    pendingOperations.add(builder.withYieldAllowed(true).build());
}

From source file:id.nci.stm_9.SelectPublicKeyFragment.java

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // This is called when a new Loader needs to be created. This
    // sample only has one Loader, so we don't care about the ID.
    Uri baseUri = KeyRings.buildPublicKeyRingsUri();

    // These are the rows that we will retrieve.
    long now = new Date().getTime() / 1000;
    String[] projection = new String[] { KeyRings._ID, KeyRings.MASTER_KEY_ID, UserIds.USER_ID,
            "(SELECT COUNT(available_keys." + Keys._ID + ") FROM " + Tables.KEYS
                    + " AS available_keys WHERE available_keys." + Keys.KEY_RING_ROW_ID + " = "
                    + KeychainDatabase.Tables.KEY_RINGS + "." + KeyRings._ID + " AND available_keys."
                    + Keys.IS_REVOKED + " = '0' AND  available_keys." + Keys.CAN_ENCRYPT + " = '1') AS "
                    + SelectKeyCursorAdapter.PROJECTION_ROW_AVAILABLE,
            "(SELECT COUNT(valid_keys." + Keys._ID + ") FROM " + Tables.KEYS
                    + " AS valid_keys WHERE valid_keys." + Keys.KEY_RING_ROW_ID + " = "
                    + KeychainDatabase.Tables.KEY_RINGS + "." + KeyRings._ID + " AND valid_keys."
                    + Keys.IS_REVOKED + " = '0' AND valid_keys." + Keys.CAN_ENCRYPT + " = '1' AND valid_keys."
                    + Keys.CREATION + " <= '" + now + "' AND " + "(valid_keys." + Keys.EXPIRY
                    + " IS NULL OR valid_keys." + Keys.EXPIRY + " >= '" + now + "')) AS "
                    + SelectKeyCursorAdapter.PROJECTION_ROW_VALID, };

    String inMasterKeyList = null;
    if (mSelectedMasterKeyIds != null && mSelectedMasterKeyIds.length > 0) {
        inMasterKeyList = KeyRings.MASTER_KEY_ID + " IN (";
        for (int i = 0; i < mSelectedMasterKeyIds.length; ++i) {
            if (i != 0) {
                inMasterKeyList += ", ";
            }/*from   ww  w  .ja v  a 2  s .c  o m*/
            inMasterKeyList += DatabaseUtils.sqlEscapeString("" + mSelectedMasterKeyIds[i]);
        }
        inMasterKeyList += ")";
    }

    // if (searchString != null && searchString.trim().length() > 0) {
    // String[] chunks = searchString.trim().split(" +");
    // qb.appendWhere("(EXISTS (SELECT tmp." + UserIds._ID + " FROM " + UserIds.TABLE_NAME
    // + " AS tmp WHERE " + "tmp." + UserIds.KEY_ID + " = " + Keys.TABLE_NAME + "."
    // + Keys._ID);
    // for (int i = 0; i < chunks.length; ++i) {
    // qb.appendWhere(" AND tmp." + UserIds.USER_ID + " LIKE ");
    // qb.appendWhereEscapeString("%" + chunks[i] + "%");
    // }
    // qb.appendWhere("))");
    //
    // if (inIdList != null) {
    // qb.appendWhere(" OR (" + inIdList + ")");
    // }
    // }

    String orderBy = UserIds.USER_ID + " ASC";
    if (inMasterKeyList != null) {
        // sort by selected master keys
        orderBy = inMasterKeyList + " DESC, " + orderBy;
    }

    // Now create and return a CursorLoader that will take care of
    // creating a Cursor for the data being displayed.
    return new CursorLoader(getActivity(), baseUri, projection, null, null, orderBy);
}

From source file:org.sufficientlysecure.keychain.remote.ui.SelectPublicKeyFragment.java

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    Uri baseUri = KeyRings.buildUnifiedKeyRingsUri();

    // These are the rows that we will retrieve.
    String[] projection = new String[] { KeyRings._ID, KeyRings.MASTER_KEY_ID, KeyRings.USER_ID,
            KeyRings.IS_EXPIRED, KeyRings.IS_REVOKED, KeyRings.HAS_ENCRYPT, KeyRings.VERIFIED,
            KeyRings.HAS_DUPLICATE_USER_ID, KeyRings.CREATION, };

    String inMasterKeyList = null;
    if (mSelectedMasterKeyIds != null && mSelectedMasterKeyIds.length > 0) {
        inMasterKeyList = Tables.KEYS + "." + KeyRings.MASTER_KEY_ID + " IN (";
        for (int i = 0; i < mSelectedMasterKeyIds.length; ++i) {
            if (i != 0) {
                inMasterKeyList += ", ";
            }/*ww  w.j av  a 2  s  . c  o m*/
            inMasterKeyList += DatabaseUtils.sqlEscapeString("" + mSelectedMasterKeyIds[i]);
        }
        inMasterKeyList += ")";
    }

    String orderBy = KeyRings.USER_ID + " ASC";
    if (inMasterKeyList != null) {
        // sort by selected master keys
        orderBy = inMasterKeyList + " DESC, " + orderBy;
    }
    String where = null;
    String whereArgs[] = null;
    if (mQuery != null) {
        String[] words = mQuery.trim().split("\\s+");
        whereArgs = new String[words.length];
        for (int i = 0; i < words.length; ++i) {
            if (where == null) {
                where = "";
            } else {
                where += " AND ";
            }
            where += KeyRings.USER_ID + " LIKE ?";
            whereArgs[i] = "%" + words[i] + "%";
        }
    }

    // Now create and return a CursorLoader that will take care of
    // creating a Cursor for the data being displayed.
    return new CursorLoader(getActivity(), baseUri, projection, where, whereArgs, orderBy);
}

From source file:at.bitfire.davdroid.mirakel.resource.LocalCalendar.java

public void deleteAllExceptRemoteNames(Resource[] remoteResources) {
    String where;//  w w w  .  java  2 s . c  om

    if (remoteResources.length != 0) {
        List<String> sqlFileNames = new LinkedList<String>();
        for (Resource res : remoteResources)
            sqlFileNames.add(DatabaseUtils.sqlEscapeString(res.getName()));
        where = entryColumnRemoteName() + " NOT IN (" + StringUtils.join(sqlFileNames, ",") + ")";
    } else
        where = entryColumnRemoteName() + " IS NOT NULL";

    Builder builder = ContentProviderOperation.newDelete(entriesURI()).withSelection(
            entryColumnParentID() + "=? AND (" + where + ")", new String[] { String.valueOf(id) });
    pendingOperations.add(builder.withYieldAllowed(true).build());
}

From source file:com.granita.icloudcalsync.resource.LocalCalendar.java

public void deleteAllExceptRemoteNames(Resource[] remoteResources) {
    List<String> sqlFileNames = new LinkedList<>();
    for (Resource res : remoteResources)
        sqlFileNames.add(DatabaseUtils.sqlEscapeString(res.getName()));

    // delete master events
    String where = entryColumnParentID() + "=?";
    where += sqlFileNames.isEmpty() ? " AND " + entryColumnRemoteName() + " IS NOT NULL" : // don't retain anything (delete all)
            " AND " + entryColumnRemoteName() + " NOT IN (" + StringUtils.join(sqlFileNames, ",") + ")"; // retain by remote file name
    if (sqlFilter != null)
        where += " AND (" + sqlFilter + ")";
    pendingOperations.add(ContentProviderOperation.newDelete(entriesURI())
            .withSelection(where, new String[] { String.valueOf(id) }).build());

    // delete exceptions, too
    where = entryColumnParentID() + "=?";
    where += sqlFileNames.isEmpty() ? " AND " + Events.ORIGINAL_SYNC_ID + " IS NOT NULL" : // don't retain anything (delete all)
            " AND " + Events.ORIGINAL_SYNC_ID + " NOT IN (" + StringUtils.join(sqlFileNames, ",") + ")"; // retain by remote file name
    pendingOperations.add(ContentProviderOperation.newDelete(entriesURI())
            .withSelection(where, new String[] { String.valueOf(id) }).withYieldAllowed(true).build());
}

From source file:org.leopub.mat.model.User.java

public boolean updateInbox(JSONArray arr) throws NetworkDataException {
    try {/*from  w  w  w  . j  a  v  a 2 s  .  c o  m*/
        int n = arr.length();
        for (int i = 0; i < n; i++) {
            JSONObject obj = arr.getJSONObject(i);
            String query = String.format(
                    "INSERT OR REPLACE INTO `inbox` VALUES('%s', '%s', '%s', '%s', '%s', %s, %s, %s, %s, '%s', '%s');",
                    obj.getString("msg_id"), obj.getString("src_id"), getContactTitle(obj.getString("src_id")),
                    obj.getString("param"), obj.getInt("type"),
                    DatabaseUtils.sqlEscapeString(obj.getString("start_time")),
                    DatabaseUtils.sqlEscapeString(obj.getString("end_time")),
                    DatabaseUtils.sqlEscapeString(obj.getString("place")),
                    DatabaseUtils.sqlEscapeString(obj.getString("text")), obj.getString("status"),
                    obj.getString("timestamp"));
            Logger.d("SQL", query);
            mDatabase.execSQL(query);
        }
        return n != 0;
    } catch (JSONException e) {
        throw new NetworkDataException("JSON/Inbox:" + e.getMessage());
    }
}

From source file:at.bitfire.davdroid.resource.LocalCalendar.java

public int deleteAllExceptRemoteNames(Resource[] remoteResources) throws LocalStorageException {
    List<String> sqlFileNames = new LinkedList<>();
    for (Resource res : remoteResources)
        sqlFileNames.add(DatabaseUtils.sqlEscapeString(res.getName()));

    // delete master events
    String where = entryColumnParentID() + "=?";
    where += sqlFileNames.isEmpty() ? " AND " + entryColumnRemoteName() + " IS NOT NULL" : // don't retain anything (delete all)
            " AND " + entryColumnRemoteName() + " NOT IN (" + StringUtils.join(sqlFileNames, ",") + ")"; // retain by remote file name
    if (sqlFilter != null)
        where += " AND (" + sqlFilter + ")";
    pendingOperations.add(ContentProviderOperation.newDelete(entriesURI())
            .withSelection(where, new String[] { String.valueOf(id) }).build());

    // delete exceptions, too
    where = entryColumnParentID() + "=?";
    where += sqlFileNames.isEmpty() ? " AND " + Events.ORIGINAL_SYNC_ID + " IS NOT NULL" : // don't retain anything (delete all)
            " AND " + Events.ORIGINAL_SYNC_ID + " NOT IN (" + StringUtils.join(sqlFileNames, ",") + ")"; // retain by remote file name
    pendingOperations.add(ContentProviderOperation.newDelete(entriesURI())
            .withSelection(where, new String[] { String.valueOf(id) }).withYieldAllowed(true).build());
    return commit();
}

From source file:org.thialfihar.android.apg.ui.SelectPublicKeyFragment.java

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // This is called when a new Loader needs to be created. This
    // sample only has one Loader, so we don't care about the ID.
    Uri baseUri = KeyRings.buildPublicKeyRingsUri();

    // These are the rows that we will retrieve.
    long now = new Date().getTime() / 1000;
    String[] projection = new String[] { KeyRings._ID, KeyRings.MASTER_KEY_ID, UserIds.USER_ID,
            "(SELECT COUNT(available_keys." + Keys._ID + ") FROM " + Tables.KEYS
                    + " AS available_keys WHERE available_keys." + Keys.KEY_RING_ROW_ID + " = "
                    + KeychainDatabase.Tables.KEY_RINGS + "." + KeyRings._ID + " AND available_keys."
                    + Keys.IS_REVOKED + " = '0' AND  available_keys." + Keys.CAN_ENCRYPT + " = '1') AS "
                    + SelectKeyCursorAdapter.PROJECTION_ROW_AVAILABLE,
            "(SELECT COUNT(valid_keys." + Keys._ID + ") FROM " + Tables.KEYS
                    + " AS valid_keys WHERE valid_keys." + Keys.KEY_RING_ROW_ID + " = "
                    + KeychainDatabase.Tables.KEY_RINGS + "." + KeyRings._ID + " AND valid_keys."
                    + Keys.IS_REVOKED + " = '0' AND valid_keys." + Keys.CAN_ENCRYPT + " = '1' AND valid_keys."
                    + Keys.CREATION + " <= '" + now + "' AND " + "(valid_keys." + Keys.EXPIRY
                    + " IS NULL OR valid_keys." + Keys.EXPIRY + " >= '" + now + "')) AS "
                    + SelectKeyCursorAdapter.PROJECTION_ROW_VALID, };

    String inMasterKeyList = null;
    if (mSelectedMasterKeyIds != null && mSelectedMasterKeyIds.length > 0) {
        inMasterKeyList = KeyRings.MASTER_KEY_ID + " IN (";
        for (int i = 0; i < mSelectedMasterKeyIds.length; ++i) {
            if (i != 0) {
                inMasterKeyList += ", ";
            }/*from w ww  .j av  a2 s  .com*/
            inMasterKeyList += DatabaseUtils.sqlEscapeString("" + mSelectedMasterKeyIds[i]);
        }
        inMasterKeyList += ")";
    }

    String orderBy = UserIds.USER_ID + " ASC";
    if (inMasterKeyList != null) {
        // sort by selected master keys
        orderBy = inMasterKeyList + " DESC, " + orderBy;
    }
    String where = null;
    String whereArgs[] = null;
    if (mCurQuery != null) {
        where = UserIds.USER_ID + " LIKE ?";
        whereArgs = new String[] { "%" + mCurQuery + "%" };
    }

    // Now create and return a CursorLoader that will take care of
    // creating a Cursor for the data being displayed.
    return new CursorLoader(getActivity(), baseUri, projection, where, whereArgs, orderBy);
}

From source file:org.leopub.mat.model.User.java

public boolean updateSent(JSONArray arr) throws NetworkDataException {
    try {//from ww  w.  j a va 2s.co  m
        int n = arr.length();
        for (int i = 0; i < n; i++) {
            JSONObject obj = arr.getJSONObject(i);
            //sent (`msg_id` integer PRIMARY KEY, `dst_str` varchar(300), `dst_title` varchar(300), `content` varchar(2048), `status` integer, `timestamp` timestamp)
            String query = String.format(
                    "INSERT OR REPLACE INTO sent VALUES('%s', '%s', '%s', '%s', %d, %s, %s, %s, %s, '%s', '%s');",
                    obj.getString("msg_id"), obj.getString("dst_str"), getGroupsTitle(obj.getString("dst_str")),
                    obj.getString("param"), obj.getInt("type"),
                    DatabaseUtils.sqlEscapeString(obj.getString("start_time")),
                    DatabaseUtils.sqlEscapeString(obj.getString("end_time")),
                    DatabaseUtils.sqlEscapeString(obj.getString("place")),
                    DatabaseUtils.sqlEscapeString(obj.getString("text")), obj.getString("status"),
                    obj.getString("timestamp"));
            Logger.d("SQL", query);
            mDatabase.execSQL(query);
        }
        return n != 0;
    } catch (JSONException e) {
        throw new NetworkDataException("JSON/Sent:" + e.getMessage());
    }
}

From source file:com.granita.icloudcalsync.resource.LocalCollection.java

/**
 * Enqueues deleting all resources except the give ones from the local collection. Requires commit().
 * @param remoteResources resources with these remote file names will be kept
 *//*  w ww  .ja  va  2s .c  o m*/
public void deleteAllExceptRemoteNames(Resource[] remoteResources) {
    final String where;

    if (remoteResources.length != 0) {
        // delete all except certain entries
        final List<String> sqlFileNames = new LinkedList<>();
        for (final Resource res : remoteResources)
            sqlFileNames.add(DatabaseUtils.sqlEscapeString(res.getName()));
        where = entryColumnRemoteName() + " NOT IN (" + StringUtils.join(sqlFileNames, ",") + ')';
    } else
        // delete all entries
        where = entryColumnRemoteName() + " IS NOT NULL";

    ContentProviderOperation.Builder builder = ContentProviderOperation.newDelete(entriesURI()).withSelection( // restrict deletion to parent collection
            entryColumnParentID() + "=? AND (" + where + ')', new String[] { String.valueOf(getId()) });
    pendingOperations.add(builder.withYieldAllowed(true).build());
}