insert or update

This commit is contained in:
arrase 2016-11-27 19:12:16 +01:00
parent 5f02561f9d
commit f153600dd1
1 changed files with 23 additions and 5 deletions

View File

@ -149,16 +149,14 @@ public class BackupUtils {
try {
JSONObject savedValues = new JSONObject(jString);
ContentValues fields = new ContentValues();
int port = savedValues.getInt(HSContentProvider.HiddenService.PORT);
fields.put(
HSContentProvider.HiddenService.NAME,
savedValues.getString(HSContentProvider.HiddenService.NAME)
);
fields.put(
HSContentProvider.HiddenService.PORT,
savedValues.getInt(HSContentProvider.HiddenService.PORT)
);
fields.put(HSContentProvider.HiddenService.PORT, port);
fields.put(
HSContentProvider.HiddenService.ONION_PORT,
@ -175,7 +173,27 @@ public class BackupUtils {
savedValues.getInt(HSContentProvider.HiddenService.CREATED_BY_USER)
);
mResolver.insert(HSContentProvider.CONTENT_URI, fields);
Cursor service = mResolver.query(
HSContentProvider.CONTENT_URI,
HSContentProvider.PROJECTION,
HSContentProvider.HiddenService.PORT + "=" + port,
null,
null
);
if (service.getCount() == 0) {
mResolver.insert(HSContentProvider.CONTENT_URI, fields);
} else {
mResolver.update(
HSContentProvider.CONTENT_URI,
fields,
HSContentProvider.HiddenService.PORT + "=" + port,
null
);
}
service.close();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(mContext, R.string.error, Toast.LENGTH_LONG).show();