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:net.e_fas.oss.tabijiman.MapsActivity.java

@Override
public void parsed(ArrayList<HashMap> data, String category) {

    String Hash = AppSetting.Encrypt(String.valueOf(data));

    switch (category) {
    case "frame":
        if (AppSetting.isHashChanged(Hash, AppSetting.getUdFrameHash())) {

            e_print("make a new frame_dic in delegate.parsed");
            AppSetting.setUdFrameHash(Hash);

        } else {//from   w  ww.  ja  va2 s .c o  m
            e_print("Already Inserted Frame Data");
            setMarker(category);
            return;
        }
    case "place":
        if (AppSetting.isHashChanged(Hash, AppSetting.getUdPlaceHash())) {

            e_print("make a new place_dic in delegate.parsed");
            AppSetting.setUdPlaceHash(Hash);

        } else {
            e_print("Already Inserted Place Data");
            setMarker(category);
            return;
        }
    }

    String sql;
    print("Success");
    print("category >> " + category);

    if (category.equals("frame")) {

        e_print("delete all data from frameTable");
        db.execSQL("delete from frame");

    } else {

        e_print("delete all data from placeTable");
        db.execSQL("delete from place");
    }

    for (HashMap test : data) {

        if (category.equals("frame") && !test.containsKey("status")) {

            print("switch - frame");

            String name = test.containsKey("name") ? test.get("name").toString() : "";
            String desc = test.containsKey("desc") ? test.get("desc").toString() : "";
            String lat = test.containsKey("lat") ? test.get("lat").toString() : "";
            String lng = test.containsKey("lng") ? test.get("lng").toString() : "";
            String img = test.containsKey("img") ? test.get("img").toString() : "";
            String area = test.containsKey("area") ? test.get("area").toString() : "";

            sql = "REPLACE INTO frame( name, desc, lat, lng, img, area, getFlag ) VALUES("
                    + DatabaseUtils.sqlEscapeString(name) + "," + DatabaseUtils.sqlEscapeString(desc) + ","
                    + DatabaseUtils.sqlEscapeString(lat) + "," + DatabaseUtils.sqlEscapeString(lng) + ","
                    + DatabaseUtils.sqlEscapeString(img) + "," + DatabaseUtils.sqlEscapeString(area) + ","
                    + DatabaseUtils.sqlEscapeString(String.valueOf(0)) + ")";

            print("SQL_QUERY >> " + sql);

            //DB()??
            SQLiteDatabase dbRead = helper.getReadableDatabase();
            //SQL
            String select_sql = "SELECT `name`, `lat`, `lng` FROM frame WHERE `name` = ? AND `lat` = ? AND `lng` = ?";
            //SQL?
            Cursor cursor = dbRead.rawQuery(select_sql, new String[] { name, lat, lng });
            //???
            cursor.moveToFirst();

            if (cursor.getCount() == 0) {
                db.execSQL(sql);
                print("frame_insert >> true");
            } else {
                print("frame_insert >> false");
            }

            cursor.close();
        } else if (category.equals("place")) {

            print("switch - place");

            String name = test.containsKey("name") ? test.get("name").toString() : "";
            String address = test.containsKey("address") ? test.get("address").toString() : "";
            String desc = test.containsKey("desc") ? test.get("desc").toString() : "";
            String lat = test.containsKey("lat") ? test.get("lat").toString() : "";
            String lng = test.containsKey("lng") ? test.get("lng").toString() : "";
            String img = test.containsKey("img") ? test.get("img").toString() : "";

            sql = "REPLACE INTO place( name, address, desc, lat, lng, img, getFlag ) VALUES("
                    + DatabaseUtils.sqlEscapeString(name) + "," + DatabaseUtils.sqlEscapeString(address) + ","
                    + DatabaseUtils.sqlEscapeString(desc) + "," + DatabaseUtils.sqlEscapeString(lat) + ","
                    + DatabaseUtils.sqlEscapeString(lng) + "," + DatabaseUtils.sqlEscapeString(img) + ","
                    + DatabaseUtils.sqlEscapeString(String.valueOf(1)) + ")";

            print("SQL_QUERY >> " + sql);

            //DB()??
            SQLiteDatabase dbRead = helper.getReadableDatabase();
            //SQL
            String select_sql = "SELECT `name`, `lat`, `lng` FROM place WHERE `name` = ? AND `lat` = ? AND `lng` = ?";
            print("select_sql >> " + select_sql);
            //SQL?
            Cursor cursor = dbRead.rawQuery(select_sql, new String[] { name, lat, lng });

            print("cursor_count >> " + cursor.getCount());

            if (cursor.getCount() == 0) {
                db.execSQL(sql);
                print("place_insert >> true");
            } else {
                print("place_insert >> false");
            }

            cursor.close();
        }
    }

    print("setMarker >> true");
    setMarker(category);
}

From source file:net.e_fas.oss.tabijiman.MapsActivity.java

public void InsertFrame(ArrayList<HashMap> datas) {

    Init_db = Init_helper.getWritableDatabase();
    Init_db.execSQL("delete from init");
    String sql;//from w  w w  .  j  a  v a  2s.c  om

    for (HashMap test : datas) {

        print("switch - init");
        String name = test.containsKey("name") ? test.get("name").toString() : "";
        String desc = test.containsKey("desc") ? test.get("desc").toString() : "";
        String lat = test.containsKey("lat") ? test.get("lat").toString() : "";
        String lng = test.containsKey("lng") ? test.get("lng").toString() : "";
        String img = test.containsKey("img") ? test.get("img").toString() : "";
        String area = test.containsKey("area") ? test.get("area").toString() : "";

        sql = "REPLACE INTO init( name, desc, lat, lng, img, area, getFlag ) VALUES("
                + DatabaseUtils.sqlEscapeString(name) + "," + DatabaseUtils.sqlEscapeString(desc) + ","
                + DatabaseUtils.sqlEscapeString(lat) + "," + DatabaseUtils.sqlEscapeString(lng) + ","
                + DatabaseUtils.sqlEscapeString(img) + "," + DatabaseUtils.sqlEscapeString(area) + ","
                + DatabaseUtils.sqlEscapeString(String.valueOf(1)) + ")";

        print("SQL_QUERY >> " + sql);

        Init_db.execSQL(sql);
    }
}

From source file:com.gelakinetic.mtgfam.helpers.CardDbAdapter.java

public Cursor PrefixSearch(String cardname, String[] returnTypes) throws FamiliarDbException {
    Cursor mCursor = null;//from  w  w  w.j ava 2  s.  c  o m
    String convertName = null;

    if (cardname != null) {
        cardname = cardname.replace(Character.toChars(0xE6)[0], Character.toChars(0xC6)[0]).trim();
        convertName = cardname.toLowerCase().replace("ae", String.valueOf(Character.toChars(0xC6)[0]));
    }

    String statement = " WHERE 1=1";

    statement += " AND (" + DATABASE_TABLE_CARDS + "." + KEY_NAME + " LIKE "
            + DatabaseUtils.sqlEscapeString(cardname + "%") + " OR " + DATABASE_TABLE_CARDS + "." + KEY_NAME
            + " LIKE " + DatabaseUtils.sqlEscapeString(convertName + "%") + ")";

    try {
        String sel = null;
        for (String s : returnTypes) {
            if (sel == null) {
                sel = DATABASE_TABLE_CARDS + "." + s + " AS " + s;
            } else {
                sel += ", " + DATABASE_TABLE_CARDS + "." + s + " AS " + s;
            }
        }
        sel += ", " + DATABASE_TABLE_SETS + "." + KEY_DATE;

        String sql = "SELECT * FROM (SELECT " + sel + " FROM " + DATABASE_TABLE_CARDS + " JOIN "
                + DATABASE_TABLE_SETS + " ON " + DATABASE_TABLE_CARDS + "." + KEY_SET + " = "
                + DATABASE_TABLE_SETS + "." + KEY_CODE + statement;

        sql += " ORDER BY " + DATABASE_TABLE_SETS + "." + KEY_DATE + ") GROUP BY " + KEY_NAME + " ORDER BY "
                + KEY_NAME + " COLLATE UNICODE";
        mCursor = mDb.rawQuery(sql, null);
    } catch (SQLiteException e) {
        throw new FamiliarDbException(e);
    } catch (IllegalStateException e) {
        throw new FamiliarDbException(e);
    }
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

From source file:com.haibison.android.anhuu.FragmentFiles.java

/**
 * Updates or inserts {@code path} into history database.
 *///from w w  w .  j ava  2  s  .co  m
private void updateDbHistory(Uri path) {
    if (BuildConfig.DEBUG)
        Log.d(CLASSNAME, "updateDbHistory() >> path = " + path);

    Calendar cal = Calendar.getInstance();
    final long beginTodayMillis = cal.getTimeInMillis() - (cal.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000
            + cal.get(Calendar.MINUTE) * 60 * 1000 + cal.get(Calendar.SECOND) * 1000);
    if (BuildConfig.DEBUG) {
        Log.d(CLASSNAME, String.format("beginToday = %s (%s)", DbUtils.formatNumber(beginTodayMillis),
                new Date(beginTodayMillis)));
        Log.d(CLASSNAME,
                String.format("endToday = %s (%s)",
                        DbUtils.formatNumber(beginTodayMillis + DateUtils.DAY_IN_MILLIS),
                        new Date(beginTodayMillis + DateUtils.DAY_IN_MILLIS)));
    }

    /*
     * Does the update and returns the number of rows updated.
     */
    long time = new Date().getTime();
    ContentValues values = new ContentValues();
    values.put(HistoryContract.COLUMN_PROVIDER_ID, BaseFileProviderUtils.getProviderId(path.getAuthority()));
    values.put(HistoryContract.COLUMN_FILE_TYPE, BaseFile.FILE_TYPE_DIRECTORY);
    values.put(HistoryContract.COLUMN_URI, path.toString());
    values.put(HistoryContract.COLUMN_MODIFICATION_TIME, DbUtils.formatNumber(time));

    int count = getActivity().getContentResolver().update(HistoryContract.genContentUri(getActivity()), values,
            String.format("%s >= '%s' and %s < '%s' and %s = %s and %s like %s",
                    HistoryContract.COLUMN_MODIFICATION_TIME, DbUtils.formatNumber(beginTodayMillis),
                    HistoryContract.COLUMN_MODIFICATION_TIME,
                    DbUtils.formatNumber(beginTodayMillis + DateUtils.DAY_IN_MILLIS),
                    HistoryContract.COLUMN_PROVIDER_ID,
                    DatabaseUtils.sqlEscapeString(values.getAsString(HistoryContract.COLUMN_PROVIDER_ID)),
                    HistoryContract.COLUMN_URI,
                    DatabaseUtils.sqlEscapeString(values.getAsString(HistoryContract.COLUMN_URI))),
            null);
    if (count <= 0) {
        values.put(HistoryContract.COLUMN_CREATE_TIME, DbUtils.formatNumber(time));
        getActivity().getContentResolver().insert(HistoryContract.genContentUri(getActivity()), values);
    }
}