Example usage for android.database.sqlite SQLiteDatabase update

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

Introduction

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

Prototype

public int update(String table, ContentValues values, String whereClause, String[] whereArgs) 

Source Link

Document

Convenience method for updating rows in the database.

Usage

From source file:com.openatk.planting.MainActivity.java

@Override
protected void onSaveInstanceState(Bundle outState) {
    if (addIsShowing == 1) {
        //Save current polygon
        List<LatLng> points = this.currentPolygon.getPoints();
        Boolean wasAnEdit = false;
        if (currentField == null) {
            //Save to outState            
            points = this.currentPolygon.getMarkers();
        } else {//from   w ww  .ja  va2s  .  co m
            currentField.setBoundary(points);
            wasAnEdit = true;
        }

        String strNewBoundary = "";
        if (points != null && points.isEmpty() == false) {
            // Generate boundary
            StringBuilder newBoundary = new StringBuilder(points.size() * 20);
            for (int i = 0; i < points.size(); i++) {
                newBoundary.append(points.get(i).latitude);
                newBoundary.append(",");
                newBoundary.append(points.get(i).longitude);
                newBoundary.append(",");
            }
            newBoundary.deleteCharAt(newBoundary.length() - 1);
            strNewBoundary = newBoundary.toString();
        }
        if (wasAnEdit) {
            // Save this field to the db
            SQLiteDatabase database = dbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(TableFields.COL_BOUNDARY, strNewBoundary);
            database.update(TableFields.TABLE_NAME, values,
                    TableFields.COL_ID + " = " + Integer.toString(currentField.getId()), null);
            dbHelper.close();
        } else {
            outState.putString("drawingBoundary", strNewBoundary);
        }
    }

    if (currentField != null)
        outState.putInt("currentField", currentField.getId());
    if (currentJob != null)
        outState.putInt("currentJob", currentJob.getId());
    if (currentOperationId != null)
        outState.putInt("currentOperationId", currentOperationId);

    outState.putInt("mCurrentState", mCurrentState);
    outState.putInt("editIsShowing", editIsShowing);
    outState.putInt("addIsShowing", addIsShowing);

    super.onSaveInstanceState(outState);
}

From source file:com.google.android.apps.muzei.provider.MuzeiProvider.java

private int updateSource(@NonNull final Uri uri, final ContentValues values, final String selection,
        final String[] selectionArgs) {
    Context context = getContext();
    if (context == null) {
        return 0;
    }/*from w  w w  .  j a  v a2 s  .c  o m*/

    // Only Muzei can set the IS_SELECTED field
    if (values.containsKey(MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED)) {
        if (!context.getPackageName().equals(getCallingPackage())) {
            Log.w(TAG, "Only Muzei can set the " + MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED
                    + " column. Ignoring the value in " + values);
            values.remove(MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED);
        }
    }

    final SQLiteDatabase db = databaseHelper.getWritableDatabase();
    String finalWhere = selection;
    String[] finalSelectionArgs = selectionArgs;
    if (MuzeiProvider.uriMatcher.match(uri) == SOURCE_ID) {
        // If the incoming URI matches a single source ID, does the update based on the incoming data, but
        // modifies the where clause to restrict it to the particular source ID.
        finalWhere = DatabaseUtils.concatenateWhere(finalWhere,
                BaseColumns._ID + " = " + uri.getLastPathSegment());
    }
    String callingPackageName = getCallingPackage();
    if (!context.getPackageName().equals(callingPackageName)) {
        // Only allow other apps to update their own source
        finalWhere = DatabaseUtils.concatenateWhere(finalWhere,
                MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME + " LIKE ?");
        finalSelectionArgs = DatabaseUtils.appendSelectionArgs(finalSelectionArgs,
                new String[] { callingPackageName + "/%" });
    }
    int count = db.update(MuzeiContract.Sources.TABLE_NAME, values, finalWhere, finalSelectionArgs);
    if (count > 0) {
        notifyChange(uri);
    } else if (values.containsKey(MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME)) {
        insertSource(MuzeiContract.Sources.CONTENT_URI, values);
        count = 1;
    }
    return count;
}

From source file:com.openatk.fieldnotebook.MainActivity.java

@Override
public void AddFieldDone(String name, Integer acres) {
    // Check if field name is valid and doesn't exist already
    if (name.length() == 0) {
        // Tell them to input a name
        // TODO add this message to R.strings
        Toast.makeText(this, "Field name cannot be blank.", Toast.LENGTH_LONG).show();
    } else {/*from  ww w. j  a v a  2s  . com*/
        // Check if field name already exists in db
        if (FindFieldByName(name) != null && currentField == null) {
            Toast.makeText(this, "A field with this name already exists. Field names must be unique.",
                    Toast.LENGTH_LONG).show();
        } else {
            this.currentPolygon.complete();
            this.currentPolygon.setLabel(name, true);
            this.currentPolygon.setFillColor(Field.FILL_COLOR_NOT_PLANNED);

            List<LatLng> points = this.currentPolygon.getPoints();
            Boolean wasAnEdit = false;
            if (currentField == null) {
                currentField = new Field(points, map);
            } else {
                currentField.setBoundary(points);
                wasAnEdit = true;
            }
            currentField.setName(name);
            currentField.setAcres(acres);

            Log.d("MainActivity", "Acres:" + Integer.toString(acres));
            String strNewBoundary = "";
            if (points != null && points.isEmpty() == false) {
                // Generate boundary
                StringBuilder newBoundary = new StringBuilder(points.size() * 20);
                for (int i = 0; i < points.size(); i++) {
                    newBoundary.append(points.get(i).latitude);
                    newBoundary.append(",");
                    newBoundary.append(points.get(i).longitude);
                    newBoundary.append(",");
                }
                newBoundary.deleteCharAt(newBoundary.length() - 1);
                strNewBoundary = newBoundary.toString();
            }
            // Save this field to the db
            SQLiteDatabase database = dbHelper.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put(TableFields.COL_NAME, currentField.getName());
            values.put(TableFields.COL_ACRES, currentField.getAcres());
            values.put(TableFields.COL_BOUNDARY, strNewBoundary);

            //TODO only update if something changed
            values.put(TableFields.COL_HAS_CHANGED, 1);
            values.put(TableFields.COL_DATE_CHANGED, DatabaseHelper.dateToStringUTC(new Date()));

            if (wasAnEdit == false) {
                Integer insertId = (int) database.insert(TableFields.TABLE_NAME, null, values);
                currentField.setId(insertId);
            } else {
                database.update(TableFields.TABLE_NAME, values,
                        TableFields.COL_ID + " = " + Integer.toString(currentField.getId()), null);
            }
            dbHelper.close();

            // Add to list so we can catch click events
            currentField.setPolygon(this.currentPolygon);

            if (wasAnEdit == false) {
                FieldsOnMap.add(currentField);
            } else {
                for (int i = 0; i < FieldsOnMap.size(); i++) {
                    if (FieldsOnMap.get(i).getId() == currentField.getId()) {
                        FieldsOnMap.get(i).setName(name);
                        FieldsOnMap.get(i).setPolygon(this.currentPolygon);
                        FieldsOnMap.get(i).setAcres(acres);
                        FieldsOnMap.get(i).setBoundary(points);
                    }
                }
            }

            showSlider(true);

            // add or update in list view
            //if (this.fragmentListView != null) this.fragmentListView.getData();
            //this.trelloController.syncDelayed();
        }
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

/**
 * update amount of remote file references for article.
 * normally should only be used with {@code null} ("unknown") and {@code 0} (no references)
 *
 * @param id         ID of article, which should be updated
 * @param filesCount new value for remote file references (may be {@code null})
 *///from w  w w.  j a v a2 s. c  o  m
public void updateArticleCachedImages(int id, Integer filesCount) {
    if (!isDBAvailable())
        return;

    ContentValues cv = new ContentValues(1);
    if (filesCount == null)
        cv.putNull("cachedImages");
    else
        cv.put("cachedImages", filesCount);

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    try {
        db.update(TABLE_ARTICLES, cv, "_id=?", new String[] { String.valueOf(id) });
    } finally {
        writeLock(false);
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

/**
 * mark given property of given articles with given state
 *
 * @param idList set of article IDs, which should be processed
 * @param mark   mark to be set//ww w .  j a  v a2s . co  m
 * @param state  value for the mark
 * @return the number of rows affected
 */
private int markArticles(String idList, String mark, int state) {
    int ret = 0;
    if (!isDBAvailable())
        return ret;

    ContentValues cv = new ContentValues(1);
    cv.put(mark, state);

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    try {
        ret = db.update(TABLE_ARTICLES, cv, "_id IN (" + idList + ") AND ? != ?",
                new String[] { mark, String.valueOf(state) });
    } finally {
        writeLock(false);
    }

    return ret;
}

From source file:org.ttrssreader.controllers.DBHelper.java

void handlePurgeMarked(String idList, int minId, String vcat) {
    if (!isDBAvailable())
        return;//from  w w w. j  av  a  2 s .  c  o m

    long time = System.currentTimeMillis();
    ContentValues cv = new ContentValues(1);
    cv.put(vcat, 0);

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    try {
        int count = db.update(TABLE_ARTICLES, cv,
                vcat + ">0 AND _id>" + minId + " AND _id NOT IN (" + idList + ")", null);
        long timeDiff = (System.currentTimeMillis() - time);
        Log.d(TAG, String.format("Marked %s articles %s=0 (%s ms)", count, vcat, timeDiff));
    } finally {
        writeLock(false);
    }
}

From source file:me.piebridge.bible.Bible.java

/**
 * save the note for book.chapter.verse//from   w  w  w.  jav a 2 s  .c  o  m
 * @param osis book.chapter
 * @param verse verse, normally number
 * @param content note content
 * @return true if saved, false if not
 */
public boolean saveNote(String osis, String verse, String verses, String content) {
    if (content == null || content.length() == 0) {
        return false;
    }
    Note note = notes.get(verse);
    if (note == null) {
        note = new Note(verse, verses, content);
    } else if (!note.update(verses, content)) {
        return false;
    }
    notes.put(verse, note);
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    if (!isDatabaseIntegrityOk(db)) {
        File path = mContext.getDatabasePath(AnnotationsDatabaseHelper.DATABASE_NAME);
        if (path != null && path.delete()) {
            return saveNote(osis, verse, verses, content);
        }
        return false;
    }
    ContentValues values = note.getContentValues();
    values.put(AnnotationsDatabaseHelper.COLUMN_OSIS, osis);
    Long id = note.getId();
    if (id == null) {
        id = db.insert(AnnotationsDatabaseHelper.TABLE_ANNOTATIONS, null, values);
        note.setId(id);
    } else {
        db.update(AnnotationsDatabaseHelper.TABLE_ANNOTATIONS, values,
                AnnotationsDatabaseHelper.COLUMN_ID + " = ?", new String[] { String.valueOf(id) });
    }
    return true;
}

From source file:com.openatk.planting.MainActivity.java

@Override
public void AddFieldDelete() {
    //Delete the current field
    if (this.currentField != null) {
        //Delete field from database
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(TableFields.COL_DELETED, 1);
        values.put(TableFields.COL_HAS_CHANGED, 1);
        values.put(TableFields.COL_DATE_CHANGED, DatabaseHelper.dateToStringUTC(new Date()));
        String where = TableFields.COL_ID + " = " + Integer.toString(currentField.getId());
        database.update(TableFields.TABLE_NAME, values, where, null);

        dbHelper.close();/*from  w w  w  . j a  v a2s .  co m*/
        for (int i = 0; i < FieldsOnMap.size(); i++) {
            if (FieldsOnMap.get(i).getId() == currentField.getId()) {
                FieldsOnMap.remove(i);
            }
        }
        currentField = null;
        this.trelloController.syncDelayed();
    }
    //Remove polygon
    if (this.currentPolygon != null) {
        this.currentPolygon.delete();
        this.currentPolygon = null;
    }
    hideAdd(true);
    if (this.fragmentListView != null)
        this.fragmentListView.getData();
}

From source file:edu.cens.loci.provider.LociDbUtils.java

/**
 * /*w  w w .j a  v  a 2  s .c o  m*/
 * @param visit Visit information to save
 * @return the number of rows affected
 */
public int updateWifiVisit(LociVisitWifi visit) {
    if (visit.enter == -1 || visit.exit == -1) {
        MyLog.e(LociConfig.D.ERROR, TAG, "[error] updateWifiVisit: (enter or exit is -1)"
                + String.format("enter=%d exit=%d", visit.enter, visit.exit));
        return -1;
    }

    if (visit.type != Places.TYPE_WIFI) {
        MyLog.e(LociConfig.D.ERROR, TAG, "[error] updateWifiVisit: place type is not TYPE_WIFI. " + visit.type);
        return -1;
    }

    final SQLiteDatabase db = mDbHelper.getWritableDatabase();
    ContentValues args = new ContentValues();
    args.put(Visits.ENTER, visit.enter);
    args.put(Visits.EXIT, visit.exit);
    args.put(Visits.PLACE_ID, visit.placeId);
    args.put(Visits.TYPE, visit.type);
    try {
        args.put(Visits.EXTRA1, visit.wifi.toJsonObject().toString());
    } catch (JSONException e) {
        MyLog.e(LociConfig.D.JSON, TAG, "[error] addWifiVisit: json error (parsing wifi to json failed).");
        e.printStackTrace();
        return -1;
    }
    args.put(Visits.EXTRA2, visit.getRecognitionResults());

    MyLog.d(LociConfig.D.DB.DEBUG, TAG,
            String.format("[DB] (updateWifiVisit) visitId=%d, enter=%s, exit=%s, placeId=%d", visit.visitId,
                    MyDateUtils.getTimeFormatMedium(visit.enter), MyDateUtils.getTimeFormatMedium(visit.exit),
                    visit.placeId));

    return db.update(Tables.VISITS, args, Visits._ID + "=" + visit.visitId, null);
}

From source file:me.piebridge.bible.Bible.java

public boolean saveHighlight(String osis, String verses) {
    if (highlighted.equals(verses)) {
        return false;
    }/*from  w w  w  .j  ava 2  s . c o  m*/
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    if (!isDatabaseIntegrityOk(db)) {
        File path = mContext.getDatabasePath(AnnotationsDatabaseHelper.DATABASE_NAME);
        if (path != null && path.delete()) {
            return saveHighlight(osis, verses);
        }
        return false;
    }
    ContentValues values = new ContentValues();
    values.put(AnnotationsDatabaseHelper.COLUMN_OSIS, osis);
    values.put(AnnotationsDatabaseHelper.COLUMN_TYPE, "highlight");
    values.put(AnnotationsDatabaseHelper.COLUMN_VERSES, verses);
    if (highlightId == null) {
        highlightId = db.insert(AnnotationsDatabaseHelper.TABLE_ANNOTATIONS, null, values);
    } else {
        db.update(AnnotationsDatabaseHelper.TABLE_ANNOTATIONS, values,
                AnnotationsDatabaseHelper.COLUMN_ID + " = ?", new String[] { String.valueOf(highlightId) });
    }
    return true;
}