Skip to content

Instantly share code, notes, and snippets.

@ryansgot
Created May 22, 2018 18:02
Show Gist options
  • Save ryansgot/6a90fff5de7696f6879a1dd1b67b6c1d to your computer and use it in GitHub Desktop.
Save ryansgot/6a90fff5de7696f6879a1dd1b67b6c1d to your computer and use it in GitHub Desktop.
Quickstart Upsert Query
// java version
// supposing there is at least one record with first_name="FS"
// and last_name="Ryan", this code will perform an update to
// all matching records. Otherwise, it will insert. This is
// called "upsert."
// the below assumes use of forsuredbandroid-contentprovider
// as the platform integration library. SaveResult would be
// parameterized with DirectLocator if using
// forsuredbandroid-directdb or forsuredbjdbc
SaveResult<Uri> saveResult = ForSure.employeesTable()
.find().byFirstName("FS")
.and().byLastName("Ryan")
.then()
.set()
.firstName("First")
.lastName("Last")
.save();
String msg = "SaveResult{exception: " + saveResult.exception(); // <-- null when upsert succeeds
msg += "; inserted: " + saveResult.inserted(); // <-- null on update, non-null on insert
msg += "; rowsAffected: " + saveResult.rowsAffected() + "}" ; // <-- 1 or more on update, 1 on insert
Log.i("forsuredb-example", msg);
// kotlin version
// supposing there is at least one record with first_name="FS"
// and last_name="Ryan", this code will perform an update to
// all matching records. Otherwise, it will insert. This is
// called "upsert."
val saveResult = ForSure.employeesTable()
.find().byFirstName("FS")
.and().byLastName("Ryan")
.then()
.set()
.firstName("First")
.lastName("Last")
.save()
var msg = "SaveResult{exception: ${saveResult.exception()}" // <-- null when upsert succeeds
msg += "; inserted: ${saveResult.inserted()}" // <-- null on update, non-null on insert
msg += "; rowsAffected: ${saveResult.rowsAffected()}}" // <-- 1 or more on update, 1 on insert
Log.i("forsuredb-example", msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment