Example usage for android.database.sqlite SQLiteDatabase delete

List of usage examples for android.database.sqlite SQLiteDatabase delete

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase delete.

Prototype

public int delete(String table, String whereClause, String[] whereArgs) 

Source Link

Document

Convenience method for deleting rows in the database.

Usage

From source file:com.example.google.touroflondon.data.TourDbHelper.java

/**
 * Extract POI data from a {@link JSONArray} of points of interest and add
 * it to the POI table./* w ww  . jav  a  2s .  c o m*/
 * 
 * @param data
 */
public void loadPois(JSONArray data) throws JSONException {

    SQLiteDatabase db = this.getWritableDatabase();

    // empty the POI table to remove all existing data
    db.delete(TourContract.PoiEntry.TABLE_NAME, null, null);

    // need to complete transaction first to clear data
    db.close();

    // begin the insert transaction
    db = this.getWritableDatabase();
    db.beginTransaction();

    // Loop over each point of interest in array
    for (int i = 0; i < data.length(); i++) {
        JSONObject poi = data.getJSONObject(i);

        // Extract POI properties
        final String title = poi.getString("title");
        final String type = poi.getString("type");
        final String description = poi.getString("description");
        final String pictureUrl = poi.getString("pictureUrl");
        final String pictureAttr = poi.getString("pictureAttr");

        // Location
        JSONObject location = poi.getJSONObject("location");
        final double lat = location.getDouble("lat");
        final double lng = location.getDouble("lng");

        // Create content values object for insert
        ContentValues cv = new ContentValues();
        cv.put(TourContract.PoiEntry.COLUMN_NAME_TITLE, title);
        cv.put(TourContract.PoiEntry.COLUMN_NAME_TYPE, type);
        cv.put(TourContract.PoiEntry.COLUMN_NAME_DESCRIPTION, description);
        cv.put(TourContract.PoiEntry.COLUMN_NAME_PICTURE_URL, pictureUrl);
        cv.put(TourContract.PoiEntry.COLUMN_NAME_LOCATION_LAT, lat);
        cv.put(TourContract.PoiEntry.COLUMN_NAME_LOCATION_LNG, lng);
        cv.put(TourContract.PoiEntry.COLUMN_NAME_PICTURE_ATTR, pictureAttr);

        // Insert data
        db.insert(TourContract.PoiEntry.TABLE_NAME, null, cv);
    }

    // All insert statement have been submitted, mark transaction as
    // successful
    db.setTransactionSuccessful();

    if (db != null) {
        db.endTransaction();
    }

}

From source file:com.example.android.touroflondon.data.TourDbHelper.java

/**
 * Extract POI data from a {@link JSONArray} of points of interest and add it to the POI table.
 *
 * @param data//from   w  w  w .  j a v a  2s. c  o  m
 */
public void loadPois(JSONArray data) throws JSONException {

    SQLiteDatabase db = this.getWritableDatabase();

    // empty the POI table to remove all existing data
    db.delete(TourContract.PoiEntry.TABLE_NAME, null, null);

    // need to complete transaction first to clear data
    db.close();

    // begin the insert transaction
    db = this.getWritableDatabase();
    db.beginTransaction();

    // Loop over each point of interest in array
    for (int i = 0; i < DataStore.getAllCoupons().size(); i++) {
        Coupon coupon = DataStore.getAllCoupons().get(i);
        //Extract POI properties
        final String storeName = coupon.getStoreName();
        String details = coupon.getTitle();
        details = details.replace("<h1>", "").replace("</h1>", "");

        final Double lat = coupon.getLatitude();
        final Double lng = coupon.getLongitude();
        /* 
        /final String type = poi.getString("type");
         final String description = poi.getString("description");
         final String pictureUrl = poi.getString("pictureUrl");
         final String pictureAttr = poi.getString("pictureAttr");
                
         // Location
         JSONObject location = poi.getJSONObject("location");
         final double lat = location.getDouble("lat");
         final double lng = location.getDouble("lng");
         */
        // Create content values object for insert
        ContentValues cv = new ContentValues();
        cv.put(TourContract.PoiEntry.COLUMN_NAME_TITLE, storeName);
        cv.put(TourContract.PoiEntry.COLUMN_NAME_TYPE, "LANDMARK");
        cv.put(TourContract.PoiEntry.COLUMN_NAME_DESCRIPTION, details);
        cv.put(TourContract.PoiEntry.COLUMN_NAME_LOCATION_LAT, lat);
        cv.put(TourContract.PoiEntry.COLUMN_NAME_LOCATION_LNG, lng);

        // Insert data
        db.insert(TourContract.PoiEntry.TABLE_NAME, null, cv);
    }

    // All insert statement have been submitted, mark transaction as successful
    db.setTransactionSuccessful();

    if (db != null) {
        db.endTransaction();
    }

}

From source file:com.elixsr.portforwarder.ui.preferences.SettingsFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);

    forwardingManager = ForwardingManager.getInstance();
    localBroadcastManager = LocalBroadcastManager.getInstance(getActivity().getBaseContext());

    // Get tracker.
    tracker = ((FwdApplication) getActivity().getApplication()).getDefaultTracker();

    clearRulesButton = (Preference) findPreference(getString(R.string.pref_clear_rules));

    clearRulesButton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override//from w w  w  . ja va2  s.c  o m
        public boolean onPreferenceClick(Preference preference) {
            //code for what you want it to do

            new AlertDialog.Builder(getActivity()).setTitle("Delete all Rules")
                    .setMessage("Are you sure you want to delete all rules?")
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            // set up the database
                            SQLiteDatabase db = new RuleDbHelper(getActivity()).getReadableDatabase();

                            db.delete(RuleContract.RuleEntry.TABLE_NAME, null, null);

                            db.close();

                            clearRulesButton.setEnabled(false);

                            // Build and send an Event.
                            tracker.send(new HitBuilders.EventBuilder().setCategory(CATEGORY_RULES)
                                    .setAction(ACTION_DELETE).setLabel(LABEL_DELETE_RULE).build());

                            Toast.makeText(getActivity(), CLEAR_RULES_COMPLETE_MESSAGE, Toast.LENGTH_SHORT)
                                    .show();
                        }
                    }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // do nothing
                        }
                    }).show();
            return true;
        }
    });

    versionNamePreference = (Preference) findPreference(getString(R.string.pref_version));

    // set up click of help button - show webview
    //        Preference helpButton = (Preference) findPreference(getString(R.string.pref_help_link));
    //        helpButton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
    //            @Override
    //            public boolean onPreferenceClick(Preference preference) {
    //                //code for what you want it to do
    //                Intent helpActivityIntent = new Intent(getActivity(), HelpActivity.class);
    //                startActivity(helpActivityIntent);
    //                return true;
    //            }
    //        });

    // set up click of about elixsr button - show webview
    Preference aboutElixsrButton = (Preference) findPreference(getString(R.string.pref_about_link));
    aboutElixsrButton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            //code for what you want it to do
            Intent aboutActivityIntent = new Intent(getActivity(), AboutElixsrActivity.class);
            startActivity(aboutActivityIntent);
            return true;
        }
    });

    changeThemeToggle = (Preference) findPreference(getString(R.string.pref_dark_theme));

}

From source file:syncthing.android.settings.AppSettings.java

public void removeCredentials(Credentials creds) {
    SQLiteDatabase _db = db.getWritableDatabase();
    Cursor c = null;// ww  w.ja v  a  2s .co m
    try {
        _db.beginTransaction();
        _db.delete(CredentialsDB.SCHEMA.TABLE, credentialsDeviceIdSel, new String[] { creds.id });
        c = _db.query(CredentialsDB.SCHEMA.TABLE, idCols, credentialsDefaultSel, null, null, null, null);
        if (c != null && c.getCount() == 0) {
            c.close();
            //no default set a new one
            c = _db.query(CredentialsDB.SCHEMA.TABLE, idCols, null, null, null, null, null);
            if (c != null && c.moveToFirst()) {
                ContentValues cv = new ContentValues();
                cv.put(CredentialsDB.SCHEMA.DEFAULT, 1);
                _db.update(CredentialsDB.SCHEMA.TABLE, cv, idSel, new String[] { c.getString(0) });
            }
        }
        _db.setTransactionSuccessful();
    } finally {
        _db.endTransaction();
        if (c != null)
            c.close();
    }
}

From source file:io.vit.vitio.Managers.ConnectDatabase.java

public void clear() {
    SQLiteDatabase sqLiteDatabase = getWritableDatabase();
    String deleteQuery = "DELETE FROM " + TABLE_COURSES + ";";
    sqLiteDatabase.delete(TABLE_COURSES, "1", null);
    Cursor cursor = sqLiteDatabase.rawQuery(deleteQuery, null);
    cursor.close();//w ww. jav a  2 s . c om
    sqLiteDatabase.close();
}

From source file:com.renjunzheng.vendingmachine.MyGcmListenerService.java

private void updateStorageInfo(String updated_info) {

    //as far as I think, this should receive all the information about all four products
    //so whenever we substitute some product, the original one will not be kept in there
    //or we need some level of delete functionality? or do we need a sync adapter?
    //is this a good idea? what happens when the number of item increases?

    try {/* w  ww. j ava  2s. c  o m*/
        DataDbHelper dbHelper = new DataDbHelper(this);
        SQLiteDatabase database = dbHelper.getWritableDatabase();

        database.delete(DataContract.ItemEntry.TABLE_NAME, null, null);
        database.execSQL(
                "DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" + DataContract.ItemEntry.TABLE_NAME + "'");

        //get this valueArray from the string
        JSONArray valueArray = new JSONArray(updated_info);
        for (int lc = 0; lc < valueArray.length(); ++lc) {
            JSONObject infoJson = valueArray.getJSONObject(lc);
            //everything is the same as following code
            ContentValues newValues = new ContentValues();
            newValues.put(DataContract.ItemEntry.COLUMN_REMAINING_NUM, infoJson.getInt("remaining_num"));
            newValues.put(DataContract.ItemEntry.COLUMN_SHORT_DESC, infoJson.getString("short_desc"));
            newValues.put(DataContract.ItemEntry.COLUMN_PRICE, infoJson.getString("item_price"));
            newValues.put(DataContract.ItemEntry.COLUMN_ITEM_NAME, infoJson.getString("item_name"));
            Uri returnedUri = getContentResolver().insert(DataContract.ItemEntry.CONTENT_URI, newValues);
            Log.i(TAG, "inserted row num " + ContentUris.parseId(returnedUri));
        }

        database.close();
        dbHelper.close();
    } catch (JSONException e) {
        Log.e(TAG, "error when parsing Json");
    }
}

From source file:com.dm.wallpaper.board.databases.Database.java

public void deleteWallpapers() {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete("SQLITE_SEQUENCE", "NAME = ?", new String[] { TABLE_WALLPAPERS });
    db.delete(TABLE_WALLPAPERS, null, null);
    db.close();/*from  w w w. ja  v  a2  s.com*/
}

From source file:com.dm.wallpaper.board.databases.Database.java

public void deleteCategories() {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete("SQLITE_SEQUENCE", "NAME = ?", new String[] { TABLE_CATEGORIES });
    db.delete(TABLE_CATEGORIES, null, null);
    db.close();// ww w.  j  a  va 2  s. c o m
}

From source file:at.bitfire.nophonespam.BlacklistActivity.java

protected void deleteSelectedNumbers() {
    final List<String> numbers = new LinkedList<>();

    SparseBooleanArray checked = list.getCheckedItemPositions();
    for (int i = checked.size() - 1; i >= 0; i--)
        if (checked.valueAt(i)) {
            int position = checked.keyAt(i);
            numbers.add(adapter.getItem(position).number);
        }// w  w  w .  jav a 2s  . co  m

    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            DbHelper dbHelper = new DbHelper(BlacklistActivity.this);
            try {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                for (String number : numbers)
                    db.delete(Number._TABLE, Number.NUMBER + "=?", new String[] { number });
            } finally {
                dbHelper.close();
            }

            getLoaderManager().restartLoader(0, null, BlacklistActivity.this);
            return null;
        }
    }.execute();
}

From source file:com.shalzz.attendance.DatabaseHandler.java

/**
 * Delete all tables and create them again
 * *//*from  ww w. j a  va2 s .  c o m*/
public void resetTables() {
    SQLiteDatabase db = this.getWritableDatabase();
    // Delete All Rows
    db.delete(Subject.TABLE_NAME, "1", null);
    db.delete(Period.TABLE_NAME, "1", null);
    db.delete(User.TABLE_NAME, "1", null);
    db.delete(AbsentDate.TABLE_NAME, "1", null);
    db.close();
}