# update

Executes an `UPDATE` or `DELETE` query and returns the **number of affected rows**.

***

## Signature

```lua
-- Callback
PG.update(query, parameters?, callback)

-- Sync
local affectedRows = PG.update.await(query, parameters?)
```

### Parameters

| Name         | Type                             | Description                              |
| ------------ | -------------------------------- | ---------------------------------------- |
| `query`      | `string`                         | `UPDATE` or `DELETE` SQL statement.      |
| `parameters` | `table?`                         | Array or key-value table of bind values. |
| `callback`   | `function(affectedRows: number)` | Called with the number of affected rows. |

### Returns

An integer representing how many rows were modified or deleted. Returns `0` if no rows were affected.

***

## Examples

**Update a single column:**

```lua
local affected = PG.update.await(
    'UPDATE players SET job = ? WHERE id = ?',
    {'mechanic', playerId}
)

if affected == 0 then
    print('Player not found.')
end
```

**Update multiple columns with named parameters:**

```lua
PG.update.await(
    'UPDATE characters SET firstname = :first, lastname = :last WHERE citizenid = :cid',
    {first = 'John', last = 'Doe', cid = citizenId}
)
```

**Delete with callback:**

```lua
PG.update('DELETE FROM vehicles WHERE owner = ? AND plate = ?', {playerId, plate}, function(rows)
    if rows > 0 then
        print('Vehicle deleted.')
    end
end)
```

***

## Notes

* `PG.update` is the correct method for both `UPDATE` and `DELETE` statements.
* If you need the deleted/updated rows themselves, use `PG.query` with a `RETURNING *` clause:

```lua
local deleted = PG.query.await('DELETE FROM items WHERE id = ? RETURNING *', {itemId})
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://prysma-studio.gitbook.io/pry-pgadapter/lua-api/update.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
