# SQLite Upsert (Insert Or Replace)

**URL:** <https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409>\
**Category:** Databases\
**Created:** [July 18, 2018, 7:19pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409 "2018-07-18T19:19:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edwin\_van\_den\_Akker](https://forum.xojo.com/user_avatar/forum.xojo.com/edwin_van_den_akker/32/1063_2.png) [@Edwin\_van\_den\_Akker](https://forum.xojo.com/u/Edwin_van_den_Akker)\
**Post date:** [July 18, 2018, 7:19pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/1 "2018-07-18T19:19:29Z")

</div>

I have made my own SQLite framework a long time ago. Now I want to make a similar one for iOS. It just makes it just so much easier without having to write repetitive code all the time.

Now, for saving records I used to use three methods…  
1 - Save (public)  
2 - Insert (private)  
3 - Update (private)

I just simply call the save method. It checks if the PK exists. If it doesn’t, it will redirect the call to the private Insert method. If it does exist, it calls the Update method.

I have read somewhere about the [Upsert Clause in SQLite](https://sqlite.org/lang_UPSERT.html).  
Looks better. It will attempt to insert a record. If there is a PK collision, it will replace the record with new data.

The problem is, is that I use a bunch of triggers to create relationships. If a “parent record” is deleted, it will delete all associated records in other tables.  
When the SQLite performs the Replace, it actually deletes the record and inserts a new one, with the new data.

The problem is that I will lose all the data in the associated tables since Replace actually deletes a record.

Are there any folks out there having experience with this kind of stuff? If so, do you have any suggestions? Or should I just stick with my old code?

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [July 18, 2018, 7:24pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/2 "2018-07-18T19:24:40Z")

</div>

The newer versions of SQLite support an ON CONFLICT clause to INSERT that will do what you want. If there is a conflict, e.g., a field or fields that are supposed to unique, the statement will execute the instructions in ON CONFLICT instead.

The current SQLite plugin does not yet support this version, at least on the desktop, but once it does, this will do what you want.

[https://sqlite.org/syntax/upsert-clause.html](https://sqlite.org/syntax/upsert-clause.html)

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [July 18, 2018, 7:27pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/3 "2018-07-18T19:27:49Z")

</div>

UPSERT syntax was added to SQLite with version 3.24.0 (2018-06-04), so it is brand new.

---

<div class="post-metadata">

**Author:** ![Edwin\_van\_den\_Akker](https://forum.xojo.com/user_avatar/forum.xojo.com/edwin_van_den_akker/32/1063_2.png) [@Edwin\_van\_den\_Akker](https://forum.xojo.com/u/Edwin_van_den_Akker)\
**Post date:** [July 18, 2018, 7:28pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/4 "2018-07-18T19:28:56Z")

</div>

> [@396838:@Kem Tekinay](#):
>
> The newer versions of SQLite support an ON CONFLICT clause to INSERT that will do what you want.

Thanks!!!  
I have seen that page, as stated in my OP. I assume I just use UPDATE instead of REPLACE…

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [July 18, 2018, 7:30pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/5 "2018-07-18T19:30:38Z")

</div>

That’s what I get for skimming your original post instead of reading it entirely. 😕

---

<div class="post-metadata">

**Author:** ![Edwin\_van\_den\_Akker](https://forum.xojo.com/user_avatar/forum.xojo.com/edwin_van_den_akker/32/1063_2.png) [@Edwin\_van\_den\_Akker](https://forum.xojo.com/u/Edwin_van_den_Akker)\
**Post date:** [July 18, 2018, 7:30pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/6 "2018-07-18T19:30:57Z")

</div>

> [@396840:@Alberto De Poo](#):
>
> UPSERT syntax was added to SQLite with version 3.24.0 (2018-06-04), so it is brand new.

Yeah, I noticed. We just have to be patient and wait until Xojo is going to use the new SQLite plugin.

---

<div class="post-metadata">

**Author:** ![Edwin\_van\_den\_Akker](https://forum.xojo.com/user_avatar/forum.xojo.com/edwin_van_den_akker/32/1063_2.png) [@Edwin\_van\_den\_Akker](https://forum.xojo.com/u/Edwin_van_den_Akker)\
**Post date:** [July 18, 2018, 7:31pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/7 "2018-07-18T19:31:39Z")

</div>

> [@396843:@Kem Tekinay](#):
>
> That’s what I get for skimming your original post instead of reading it entirely. 😕

Haha… no problem… I do that all the time.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [July 18, 2018, 7:33pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/8 "2018-07-18T19:33:13Z")

</div>

BTW, we handle the problem by assigning a large negative constant to the object’s ID. When saving, it checks to see if the ID = that constant and, if so, does an insert and fills in the ID. Otherwise, it does an update using that ID.

---

<div class="post-metadata">

**Author:** ![Edwin\_van\_den\_Akker](https://forum.xojo.com/user_avatar/forum.xojo.com/edwin_van_den_akker/32/1063_2.png) [@Edwin\_van\_den\_Akker](https://forum.xojo.com/u/Edwin_van_den_Akker)\
**Post date:** [July 18, 2018, 7:38pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/9 "2018-07-18T19:38:26Z")

</div>

[quote=396846:@Kem Tekinay]

BTW, we handle the problem by assigning a large negative constant to the object’s ID. [/quote]

Actually… what if I would change my triggers.  
What if, in case the record is being replaced, I first prefix the PK with an underscore character. My PKs are all GUIDs, so, an underscore is ok.  
At the end I will remove the underscore. No harm will be done to the records that have my PK as FK… right?

---

<div class="post-metadata">

**Author:** ![Greg\_O\_Lone](https://forum.xojo.com/user_avatar/forum.xojo.com/greg_o_lone/32/49_2.png) [@Greg\_O\_Lone](https://forum.xojo.com/u/Greg_O_Lone)\
**Post date:** [July 18, 2018, 11:07pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/10 "2018-07-18T23:07:14Z")

</div>

> [@396844:@Edwin van den Akker](#):
>
> Yeah, I noticed. We just have to be patient and wait until Xojo is going to use the new SQLite plugin.

FWIW, We typically like to wait for a sqlite release to be stable for at least 6 weeks before including it in a release.

---

<div class="post-metadata">

**Author:** ![Greg\_O\_Lone](https://forum.xojo.com/user_avatar/forum.xojo.com/greg_o_lone/32/49_2.png) [@Greg\_O\_Lone](https://forum.xojo.com/u/Greg_O_Lone)\
**Post date:** [July 18, 2018, 11:08pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/11 "2018-07-18T23:08:56Z")

</div>

> [@396844:@Edwin van den Akker](#):
>
> Yeah, I noticed. We just have to be patient and wait until Xojo is going to use the new SQLite plugin.

FWIW, We typically like to wait for a sqlite release to be stable for at least 6 weeks before including it in a release. Its just past that point so itll be probably be considered during the next pre-alpha cycle.

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [July 18, 2018, 11:17pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/12 "2018-07-18T23:17:09Z")

</div>

Thank you Greg.

I know what you mean, just looked in release history and several times they do a point version a week or two after a release, but not this time.

---

<div class="post-metadata">

**Author:** ![Maximilian\_Tyrtania](https://forum.xojo.com/user_avatar/forum.xojo.com/maximilian_tyrtania/32/294_2.png) [@Maximilian\_Tyrtania](https://forum.xojo.com/u/Maximilian_Tyrtania)\
**Post date:** [July 19, 2018, 6:26am UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/13 "2018-07-19T06:26:58Z")

</div>

> [@](#):
>
> The problem is, is that I use a bunch of triggers to create relationships. If a “parent record” is deleted, it will delete all associated records in other tables.

It is a bit unusual to do that manually in a trigger. I think if you just set up your FKs the normal way like

```auto
Alter Table myRelatedTable add column myFK integer references MainTable ON DELETE CASCADE
```

you shouldn’t run into the problem you describe at all. Haven’t tested this though…

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [July 19, 2018, 6:58am UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/14 "2018-07-19T06:58:31Z")

</div>

You can always get a newer version in MBS SQL Plugin.

---

<div class="post-metadata">

**Author:** ![Dirk\_Cleenwerck](https://forum.xojo.com/user_avatar/forum.xojo.com/dirk_cleenwerck/32/533_2.png) [@Dirk\_Cleenwerck](https://forum.xojo.com/u/Dirk_Cleenwerck)\
**Post date:** [July 19, 2018, 12:28pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/15 "2018-07-19T12:28:21Z")

</div>

> [@396844:@Edwin van den Akker](#):
>
> Yeah, I noticed. We just have to be patient and wait until Xojo is going to use the new SQLite plugin.

I use the following code to do update or insert (with SQLite) in my LunaAPIDocGen program:

`Dim db As New SQLiteDatabase
db.DatabaseFile = SpecialFolder.ApplicationData.Child(APIDocDBName)
if db.Connect Then
  Dim strSQL As String
  Dim strAPIDocsBranch As String=tfJsonDoc.Text
  //https://stackoverflow.com/questions/15277373/sqlite-upsert-update-or-insert
  strSQL="UPDATE apidocs SET apidocsbranchjson='" + EscapeSQLData(taJsonDoc.Text) + _
  "' WHERE apidocsbranch='" + EscapeSQLData(strAPIDocsBranch) + "';"
  strSQL=strSQL + "INSERT INTO apidocs (apidocsbranch, apidocsbranchjson) SELECT '" + _
  EscapeSQLData(strAPIDocsBranch) + "', '" + EscapeSQLData(taJsonDoc.Text) + "' " + _
  "WHERE (SELECT changes() = 0);"
  //How It Works
  //The 'magic' here is you use the Where (Select Changes() = 0) clause 
  //to determine if there are any rows for the insert, 
  //and since that is based on your own Where clause, 
  //it can be for anything you define, not just key violations.
  //In the above example, if there are no changes from the update 
  ///(i.e. the record doesn't exist) then Changes() = 0 
  //so the Where clause in the Insert statement returns true 
  //and a new row is inserted with the specified data.
  //If the Update did update an existing row, then Changes() = 1, 
  //so the 'Where' clause in the Insert will now be false 
  //and thus no insert will take place.
  //No brute-force needed.
  db.SQLExecute(strSQL)
  if db.Error Then
    //handle error here
    MsgBox("Could not save the branch to apidocs.")
  End If
else
  System.DebugLog "The database could not be opened. Error: " + db.ErrorMessage
end if`

This works with the current plugin btw. No need to wait for changes.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [July 19, 2018, 12:46pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/16 "2018-07-19T12:46:43Z")

</div>

Except for the part about manually escaping the data instead of using a prepared statement (!), I like it!

[https://sqlite.org/lang\_corefunc.html#changes](https://sqlite.org/lang_corefunc.html#changes)

---

<div class="post-metadata">

**Author:** ![Ralph\_Alvy](https://forum.xojo.com/user_avatar/forum.xojo.com/ralph_alvy/32/4528_2.png) [@Ralph\_Alvy](https://forum.xojo.com/u/Ralph_Alvy)\
**Post date:** [August 7, 2018, 2:52pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/17 "2018-08-07T14:52:53Z")

</div>

> [@396860:@Greg O'Lone](#):
>
> FWIW, We typically like to wait for a sqlite release to be stable for at least 6 weeks before including it in a release. Its just past that point so itll be probably be considered during the next pre-alpha cycle.

Looks like SQLite 3.24.0 didn’t make it into 2018r2. No complaints. Just letting people know we don’t have UPSERT quite yet.

Once that finally happens, would I have to wait until CubeSQLServer updates its SQLite to 3.24.0 to be able to use UPSERT, if I run my SQLite databases under CubeSQLServer?

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [August 7, 2018, 4:15pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/18 "2018-08-07T16:15:42Z")

</div>

It was not typically by exception ! 😉

---

<div class="post-metadata">

**Author:** ![Ralph\_Alvy](https://forum.xojo.com/user_avatar/forum.xojo.com/ralph_alvy/32/4528_2.png) [@Ralph\_Alvy](https://forum.xojo.com/u/Ralph_Alvy)\
**Post date:** [August 7, 2018, 4:40pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/19 "2018-08-07T16:40:06Z")

</div>

> [@399601:@Emile Schwarz](#):
>
> It was not typically by exception ! 😉

What does that mean?

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [August 7, 2018, 4:44pm UTC](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409/20 "2018-08-07T16:44:54Z")

</div>

You can always use [MBS Xojo SQL Plugin](http://www.monkeybreadsoftware.de/xojo/plugin-sql.shtml) for a more recent SQLite version.

[Next page](https://forum.xojo.com/t/sqlite-upsert-insert-or-replace/44409.md?page=2)
