Created
May 22, 2018 18:02
-
-
Save ryansgot/6a90fff5de7696f6879a1dd1b67b6c1d to your computer and use it in GitHub Desktop.
Quickstart Upsert Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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