# Updating a single record

**URL:** <https://forum.xojo.com/t/updating-a-single-record/15818>\
**Category:** Databases\
**Created:** [April 17, 2014, 2:45pm UTC](https://forum.xojo.com/t/updating-a-single-record/15818 "2014-04-17T14:45:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger\_Clary](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@Roger\_Clary](https://forum.xojo.com/u/Roger_Clary)\
**Post date:** [April 17, 2014, 2:45pm UTC](https://forum.xojo.com/t/updating-a-single-record/15818/1 "2014-04-17T14:45:31Z")

</div>

Can I update a single record in a sqLite DB in one shot like…

```
[code]rs.Edit
rs.Field("W").IntegerValue = rs.Field("W").IntegerValue
rs.Update[/code]

```

or do I have to do something like…

```
[code]curTot = rs.Field("W").IntegerValue
curTot = curTot + 1

rs.Edit
rs.Field("W").IntegerValue = curTot
rs.Update[/code]
```

---

<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:** [April 17, 2014, 4:00pm UTC](https://forum.xojo.com/t/updating-a-single-record/15818/2 "2014-04-17T16:00:39Z")

</div>

it can, but I would use a +1 in the first example…

---

<div class="post-metadata">

**Author:** ![Roger\_Clary](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@Roger\_Clary](https://forum.xojo.com/u/Roger_Clary)\
**Post date:** [April 17, 2014, 5:00pm UTC](https://forum.xojo.com/t/updating-a-single-record/15818/3 "2014-04-17T17:00:43Z")

</div>

Thanks, Christian. Yea, poor editing on my part. The line should read…

```auto
rs.Field("W").IntegerValue = rs.Field("W").IntegerValue + 1
```

Wanted to know if that worked before testing. Thanks again.

---

<div class="post-metadata">

**Author:** ![Tomas\_Sancio](https://forum.xojo.com/user_avatar/forum.xojo.com/tomas_sancio/32/1187_2.png) [@Tomas\_Sancio](https://forum.xojo.com/u/Tomas_Sancio)\
**Post date:** [May 2, 2014, 3:35pm UTC](https://forum.xojo.com/t/updating-a-single-record/15818/4 "2014-05-02T15:35:45Z")

</div>

It’s probably cleaner (easier to read and maintain) if you use SQL when possible.

`yourdatabase.sqlExecute("UPDATE <table_name> SET w=w+1 WHERE <identification>=<value>")` where table\_name is the table where the “w” column is, identification=value is the unique id of the record.

A person that doesn’t know Xojo but knows SQL can later read what you did.

---

<div class="post-metadata">

**Author:** ![Roger\_Clary](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@Roger\_Clary](https://forum.xojo.com/u/Roger_Clary)\
**Post date:** [May 3, 2014, 7:25pm UTC](https://forum.xojo.com/t/updating-a-single-record/15818/5 "2014-05-03T19:25:39Z")

</div>

Tomas, to me that is neither cleaner nor easier. Beauty is in the eye of the beholder, I guess. Thanks for the suggestion, though.

---

<div class="post-metadata">

**Author:** ![Richard\_Duke](https://forum.xojo.com/user_avatar/forum.xojo.com/richard_duke/32/246_2.png) [@Richard\_Duke](https://forum.xojo.com/u/Richard_Duke)\
**Post date:** [May 5, 2014, 12:31am UTC](https://forum.xojo.com/t/updating-a-single-record/15818/6 "2014-05-05T00:31:51Z")

</div>

instead of yourdatabase.sqlExecute(“UPDATE \<table\_name\> SET w=w+1 WHERE =”)

i usually do

dim mySQL as string = “UPDATE \<table\_name\> SET w=w+1 WHERE =”  
yourdatabase.sqlExecute(mySQL)

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [May 5, 2014, 1:23am UTC](https://forum.xojo.com/t/updating-a-single-record/15818/7 "2014-05-05T01:23:58Z")

</div>

^^ that way if there’s something going wrong with the sql you can breakpoint to see just what’s trying to execute.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [May 5, 2014, 1:42am UTC](https://forum.xojo.com/t/updating-a-single-record/15818/8 "2014-05-05T01:42:34Z")

</div>

Be sure, under all circumstances to properly escape anything you `+` in a SQL statement. For example,

```auto
Dim sql As String = "UPDATE table SET w=w+1 WHERE id=" + Str(id)
```

is fine, a number doesn’t really need escaped. But:

```auto
Dim sql As String = "UPDATE table SET w=w+1 WHERE name=" + name
```

Is a security hole waiting to be exploited, especially in WebApps. The above should be written as a `rs.Edit/rs.Update` statement or a prepared statement only.

```auto
Dim ps As PreparedSQLStatement = db.Prepare("UPDATE table SET w=w+1 WHERE name=?")
If ps = Nil Then
    // Look at/report db.Error and db.ErrorMessage
    Return
End If
ps.SQLExecute(name)
```

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [May 5, 2014, 1:44am UTC](https://forum.xojo.com/t/updating-a-single-record/15818/9 "2014-05-05T01:44:02Z")

</div>

> [@85493:@Tim Parnell](#):
>
> ^^ that way if there’s something going wrong with the sql you can breakpoint to see just what’s trying to execute.

Looking at db.ErrorMessage should give you the information you need to fix the problem. I have found `rs.Edit/rs.Update` much less error prone than hand crafting SQL, and I love SQL.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:11pm UTC](https://forum.xojo.com/t/updating-a-single-record/15818/10 "2020-10-29T17:11:03Z")

</div>


