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.fieldnotebook.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  ww w. ja va2 s  .  c  o 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());

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

    super.onSaveInstanceState(outState);
}

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

public int updatePlaceBasic(LociPlace place) {
    final SQLiteDatabase db = mDbHelper.getWritableDatabase();
    ContentValues args = new ContentValues();
    args.put(Places.PLACE_NAME, place.name);
    args.put(Places.PLACE_STATE, place.state);
    args.put(Places.PLACE_TYPE, place.type);
    return db.update(Tables.PLACES, args, Places._ID + "=" + place.placeId, null);
}

From source file:org.kontalk.provider.UsersProvider.java

private Uri insertUser(ContentValues values, boolean offline, boolean discardName) {
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    String table = offline ? TABLE_USERS_OFFLINE : TABLE_USERS;
    long id = 0;/*from   w ww.ja v  a 2s .c  om*/

    try {
        id = db.insertOrThrow(table, null, values);
    } catch (SQLException e) {
        String jid = values.getAsString(Users.JID);
        if (jid != null) {
            // discard display_name if requested
            if (discardName) {
                values.remove(Users.DISPLAY_NAME);
                values.remove(Users.NUMBER);
            }

            db.update(table, values, Users.JID + "=?", new String[] { jid });
        }
    }

    if (id >= 0)
        return ContentUris.withAppendedId(Users.CONTENT_URI, id);
    return null;
}

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

public int changeVisitPlaceIdAll(long oldId, long newId) {
    final SQLiteDatabase db = mDbHelper.getWritableDatabase();
    ContentValues args = new ContentValues();
    args.put(Visits.PLACE_ID, newId);/*w  w  w.  j a v  a  2s . c o m*/

    MyLog.d(LociConfig.D.DB.CALL, TAG,
            String.format("[DB] change all visits placeid : (oldId=%d, newId=%d)", oldId, newId));

    return db.update(Tables.VISITS, args, Visits.PLACE_ID + "=" + oldId, null);
}

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

public int updateVisitPlaceId(long visitId, long placeId) {
    final SQLiteDatabase db = mDbHelper.getWritableDatabase();
    ContentValues args = new ContentValues();
    args.put(Visits.PLACE_ID, placeId);/*ww w.  j a va  2 s. co m*/

    MyLog.d(LociConfig.D.DB.CALL, TAG,
            String.format("[DB] change visit's placeid : (visitId=%d, placeId=%d)", visitId, placeId));

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

From source file:com.concentricsky.android.khanacademy.data.remote.LibraryUpdaterTask.java

private void mergeDbs() {
    Log.d(LOG_TAG, "update received - juggling dbs");
    // Get main database, attach temp db to it.
    SQLiteDatabase mainDb = dataService.getHelper().getWritableDatabase();
    mainDb.execSQL("attach database ? as ka_temp",
            new Object[] { dataService.getDatabasePath("ka_temp").getAbsolutePath() });

    mainDb.beginTransaction();/*from  w  w w  .  ja v  a  2s .  c  om*/
    try {

        // Maintain download status.
        String sql = "select max(download_status), dlm_id, youtube_id from video where download_status != ? group by youtube_id";
        Cursor c = mainDb.rawQuery(sql, new String[] { "" + Video.DL_STATUS_NOT_STARTED });
        Cursor c1;
        String[] videoIds = new String[c.getCount()];
        int i = 0;
        while (c.moveToNext()) {
            String youtube_id = c.getString(c.getColumnIndex("youtube_id"));
            String download_status = c.getString(c.getColumnIndex("max(download_status)"));
            long dlm_id = c.getLong(c.getColumnIndex("dlm_id"));
            videoIds[i++] = youtube_id;
            ContentValues v = new ContentValues();
            v.put("download_status", download_status);
            v.put("dlm_id", dlm_id);
            String[] idArg = new String[] { youtube_id };
            mainDb.update("ka_temp.video", v, "youtube_id = ?", idArg);

            // cursor over parent topics of this video
            sql = "select ka_temp.topic._id from ka_temp.topic, ka_temp.topicvideo, ka_temp.video where ka_temp.video.youtube_id=? and ka_temp.topicvideo.video_id=ka_temp.video.readable_id and ka_temp.topicvideo.topic_id=ka_temp.topic._id";
            c1 = mainDb.rawQuery(sql, idArg);
            Log.d(LOG_TAG, String.format("updating counts for %d topics", c1.getCount()));
            while (c1.moveToNext()) {
                String topicId = c1.getString(c1.getColumnIndex("_id"));
                DatabaseHelper.incrementDownloadedVideoCounts(mainDb, topicId, "ka_temp.topic");
            }
            c1.close();
        }
        c.close();

        mainDb.execSQL("delete from topic");
        mainDb.execSQL("insert into topic select * from ka_temp.topic");

        mainDb.execSQL("delete from topicvideo");
        mainDb.execSQL("insert into topicvideo select * from ka_temp.topicvideo");

        mainDb.execSQL("delete from video");
        mainDb.execSQL("insert into video select * from ka_temp.video");

        mainDb.setTransactionSuccessful();
    } finally {
        mainDb.endTransaction();
        mainDb.execSQL("detach database ka_temp");
    }

    Log.d(LOG_TAG, "finished juggling");
}

From source file:pl.selvin.android.syncframework.content.TableInfo.java

@SuppressLint("NewApi")
final public void SyncJSON(final HashMap<String, Object> hval, final Metadata meta, final SQLiteDatabase db) {
    int i = 0;//from  ww  w .  ja v a 2 s.c  o  m
    vals.clear();
    for (; i < columns.length; i++) {
        String column = columns[i].name;
        switch (columns[i].type) {
        case ColumnType.BLOB:
            final String str = (String) hval.get(column);
            if (str != null)
                vals.put(column, Base64.decode(str, Base64.DEFAULT));
            break;
        case ColumnType.BOOLEAN:
        case ColumnType.INTEGER:
            vals.put(column, (Long) hval.get(column));
            break;
        case ColumnType.DATETIME:
            String date = (String) hval.get(column);
            if (date != null) {
                date = sdf.format(new Date(Long.parseLong(date.substring(6, date.length() - 2))));
            }
            vals.put(column, date);
            break;
        case ColumnType.NUMERIC:
            Object obj = hval.get(column);
            if (obj instanceof Double)
                vals.put(column, (Double) obj);
            else
                vals.put(column, (Long) obj);
            break;
        default:
            vals.put(column, (String) hval.get(column));
            break;
        }
    }
    vals.put(_.uri, meta.uri);
    vals.put(_.tempId, (String) null);
    vals.put(_.isDirty, 0);
    if (meta.tempId != null) {
        db.update(name, vals, _.tempIdP, new String[] { meta.tempId });
    } else {
        db.replace(name, null, vals);
    }
}

From source file:com.openatk.fieldnotebook.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   ww w.  j a  v a 2  s  . c  o 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:com.openerp.orm.ORM.java

/**
 * Update m2 m records./*from  w  w  w.  ja  v  a 2  s . co  m*/
 * 
 * @param id
 *            the id
 * @param values
 *            the values
 * @param key
 *            the key
 * @param dbHelper
 *            the db helper
 * @param m2m
 *            the m2m
 * @param rootRow
 *            the root row
 */
private void updateM2MRecords(String id, JSONArray values, String key, BaseDBHelper dbHelper, Many2Many m2m,
        ContentValues rootRow) {
    // TODO Auto-generated method stub
    String table1 = modelToTable(dbHelper.getModelName());
    String table2 = "";
    BaseDBHelper tbl2Obj = null;
    if (m2m.isM2MObject()) {
        tbl2Obj = (BaseDBHelper) m2m.getM2mObject();
        table2 = modelToTable(tbl2Obj.getModelName());
    } else {
        table2 = modelToTable(m2m.getModel_name());
    }
    String rel_table = table1 + "_" + table2 + "_rel";
    String col1 = table1 + "_id";
    String col2 = table2 + "_id";
    String col3 = "oea_name";

    // Temp dummy helper
    BaseDBHelper newDb = generateM2MHelper(dbHelper, m2m);

    for (int i = 0; i < values.length(); i++) {
        try {
            int row_id = 0;

            if (values.get(i) instanceof JSONArray) {
                row_id = values.getJSONArray(i).getInt(0);
            }
            if (values.get(i) instanceof JSONObject) {
                JSONObject obj = (JSONObject) values.get(i);
                if (obj.has("id")) {
                    row_id = obj.getInt("id");
                }
            }
            if (values.get(i) instanceof Integer) {
                row_id = values.getInt(i);
            }

            ContentValues m2mvals = new ContentValues();
            m2mvals.put(col1, id);
            m2mvals.put(col2, row_id);
            m2mvals.put(col3, user_name);
            int res = (Integer) this
                    .search(newDb, new String[] { col1 + " = ?", "AND", col2 + "= ?", "AND", col3 + " = ?" },
                            new String[] { id, row_id + "", user_name })
                    .get("total");
            SQLiteDatabase db = getWritableDatabase();
            if (res == 0) {

                db.insert(rel_table, null, m2mvals);

                if (tbl2Obj != null) {
                    List<Integer> list = new ArrayList<Integer>();
                    list.add(row_id);
                    oe_obj.syncReferenceTables(tbl2Obj, list, false);
                }
            } else {
                db.update(rel_table, m2mvals, col1 + " = " + id + " AND " + col2 + " = " + row_id + " AND "
                        + col3 + " = '" + user_name + "' ", null);
            }
            db.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

From source file:com.odoo.orm.OModel.java

/**
 * Update./*  ww  w  .  j av a  2  s . c  om*/
 * 
 * @param updateValues
 *            the update values
 * @param where
 *            the where
 * @param whereArgs
 *            the where args
 * @return the int
 */
public int update(OValues updateValues, String where, Object[] whereArgs) {
    int affectedRows = 0;
    ContentValues values = createValues(updateValues);
    SQLiteDatabase db = getWritableDatabase();
    if (!updateValues.contains("is_dirty"))
        values.put("is_dirty", "true");
    affectedRows = db.update(getTableName(), values, getWhereClause(where), getWhereArgs(where, whereArgs));
    db.close();
    updateRelationColumns(updateValues);
    notifyDataChange(null);
    return affectedRows;
}