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:Main.java

public static String sanitizeString(String str) {
    if (!isNullOrEmpty(str)) {
        str = DatabaseUtils.sqlEscapeString(str);
        str = str.replaceAll("'", "");
        str = str.replaceAll("\"", "");
    }/*from  w ww .  j  av  a  2s  .c om*/
    return str;

}

From source file:Main.java

private static String sqlEscapeString(String aString) {
    String aReturn = "";

    if (null != aString) {
        //aReturn = aString.replace("'", "''");
        aReturn = DatabaseUtils.sqlEscapeString(aString);
        // Remove the enclosing single quotes ...
        aReturn = aReturn.substring(1, aReturn.length() - 1);
    }// ww w  . j  a v a2s  .  c  o  m

    return aReturn;
}

From source file:Main.java

protected static long findThreadId(Context context, String messageId) {
    StringBuilder sb = new StringBuilder('(');
    sb.append(Telephony.Mms.MESSAGE_ID);
    sb.append('=');
    sb.append(DatabaseUtils.sqlEscapeString(messageId));
    sb.append(" AND ");
    sb.append(Telephony.Mms.MESSAGE_TYPE);
    sb.append('=');
    sb.append(MESSAGE_TYPE_SEND_REQ);//from   w  ww .jav a  2s .c o m

    // TODO ContentResolver.query() appends closing ')' to the selection argument
    // sb.append(')');

    Cursor cursor = context.getContentResolver().query(Telephony.Mms.CONTENT_URI,
            new String[] { Telephony.Mms.THREAD_ID }, sb.toString(), null, null);

    if (cursor != null) {
        try {
            if ((cursor.getCount() == 1) && cursor.moveToFirst()) {
                return cursor.getLong(0);
            }
        } finally {
            cursor.close();
        }
    }

    return -1;
}

From source file:Main.java

private static String dumpRow(Cursor c) {
    String result = "VALUES(";

    for (int pos = 0; pos < c.getColumnCount(); pos++) {
        switch (c.getType(pos)) {
        case Cursor.FIELD_TYPE_NULL:
            result += "null";
            break;
        case Cursor.FIELD_TYPE_INTEGER:
            result += Integer.toString(c.getInt(pos));
            break;
        case Cursor.FIELD_TYPE_FLOAT:
            result += Float.toString(c.getFloat(pos));
            break;
        case Cursor.FIELD_TYPE_STRING:
            result += DatabaseUtils.sqlEscapeString(c.getString(pos));
            break;
        case Cursor.FIELD_TYPE_BLOB:
            result += "X'";
            for (byte b : c.getBlob(pos)) {
                result += String.format("%02X", b);
            }/*from w ww.  ja v a 2 s  . com*/
            result += "'";
            break;
        default:
            return "Invalid field type " + c.getType(pos) + " at position " + pos;
        }

        if (pos < c.getColumnCount() - 1) {
            result += ", ";
        }
    }
    return result + ")";
}

From source file:Main.java

public static LinkedHashMap<String, Uri> GetMatchedContactsList(Context context, String searchTerm) {
    LinkedHashMap<String, Uri> contactList = new LinkedHashMap<String, Uri>();
    ContentResolver cr = context.getContentResolver();
    String columns[] = { ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI };
    Cursor cur;/*  www.  j a  va  2  s  . c om*/
    if (searchTerm == null) {
        cur = cr.query(ContactsContract.Contacts.CONTENT_URI, columns, null, null, null);
    } else {
        cur = cr.query(ContactsContract.Contacts.CONTENT_URI, columns,
                ContactsContract.Contacts.DISPLAY_NAME + " LIKE "
                        + DatabaseUtils.sqlEscapeString("%" + searchTerm + "%"),
                null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
    }
    if (cur != null) {
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String photoURI = cur
                        .getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
                if (photoURI != null) {
                    Uri thumbUri = Uri.parse(photoURI);
                    contactList.put(name, thumbUri);
                }
            }
        }
        cur.close();
    }
    return contactList;
}

From source file:net.olejon.mdapp.MyTools.java

public String sqe(String string) {
    return DatabaseUtils.sqlEscapeString(string);
}

From source file:io.romain.passport.ui.fragments.CityListFragment.java

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    if (TextUtils.isEmpty(mQuery)) {
        return new CursorLoader(getContext(), PassportContentProvider.Cities.CONTENT_URI, null, null, null,
                null);// w ww.j  a v a 2  s  . co  m
    } else {
        return new CursorLoader(getContext(), PassportContentProvider.Cities.CONTENT_URI, null,
                City.NAME + " LIKE " + DatabaseUtils.sqlEscapeString(mQuery + "%"), null, null);
    }
}

From source file:at.bitfire.davdroid.DavService.java

@SuppressLint("MissingPermission")
void cleanupAccounts() {
    App.log.info("Cleaning up orphaned accounts");

    final OpenHelper dbHelper = new OpenHelper(this);
    try {/*from   w w w  .j  a va 2 s. c  o  m*/
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        List<String> sqlAccountNames = new LinkedList<>();
        AccountManager am = AccountManager.get(this);
        for (Account account : am.getAccountsByType(Constants.ACCOUNT_TYPE))
            sqlAccountNames.add(DatabaseUtils.sqlEscapeString(account.name));

        if (sqlAccountNames.isEmpty())
            db.delete(Services._TABLE, null, null);
        else
            db.delete(Services._TABLE,
                    Services.ACCOUNT_NAME + " NOT IN (" + TextUtils.join(",", sqlAccountNames) + ")", null);
    } finally {
        dbHelper.close();
    }
}

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

public void deleteAllExceptRemoteNames(Resource[] remoteResources) {
    String where;//from   w  w  w .j  a v  a2  s.c o  m

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

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

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

public void deleteAllExceptRemoteNames(Resource[] remoteResources) {
    String where;// w  w  w.  j  a  v a2 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(where, null);
    pendingOperations.add(builder.withYieldAllowed(true).build());
}