Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
116 lines
1.9 KiB
Markdown
116 lines
1.9 KiB
Markdown
# Updating rows
|
|
|
|
```sql
|
|
CREATE TABLE authors (
|
|
id SERIAL PRIMARY KEY,
|
|
bio text NOT NULL
|
|
);
|
|
```
|
|
|
|
## Single parameter
|
|
|
|
If your query has a single parameter, your Go method will also have a single
|
|
parameter.
|
|
|
|
The parameter syntax varies by database engine:
|
|
|
|
**PostgreSQL:**
|
|
```sql
|
|
-- name: UpdateAuthorBios :exec
|
|
UPDATE authors SET bio = $1;
|
|
```
|
|
|
|
**MySQL and SQLite:**
|
|
```sql
|
|
-- name: UpdateAuthorBios :exec
|
|
UPDATE authors SET bio = ?;
|
|
```
|
|
|
|
```go
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
type DBTX interface {
|
|
ExecContext(context.Context, string, ...any) (sql.Result, error)
|
|
}
|
|
|
|
func New(db DBTX) *Queries {
|
|
return &Queries{db: db}
|
|
}
|
|
|
|
type Queries struct {
|
|
db DBTX
|
|
}
|
|
|
|
const updateAuthorBios = `-- name: UpdateAuthorBios :exec
|
|
UPDATE authors SET bio = $1
|
|
`
|
|
|
|
func (q *Queries) UpdateAuthorBios(ctx context.Context, bio string) error {
|
|
_, err := q.db.ExecContext(ctx, updateAuthorBios, bio)
|
|
return err
|
|
}
|
|
```
|
|
|
|
## Multiple parameters
|
|
|
|
If your query has more than one parameter, your Go method will accept a
|
|
`Params` struct.
|
|
|
|
**PostgreSQL:**
|
|
```sql
|
|
-- name: UpdateAuthor :exec
|
|
UPDATE authors SET bio = $2
|
|
WHERE id = $1;
|
|
```
|
|
|
|
**MySQL and SQLite:**
|
|
```sql
|
|
-- name: UpdateAuthor :exec
|
|
UPDATE authors SET bio = ?
|
|
WHERE id = ?;
|
|
```
|
|
|
|
Note: For MySQL and SQLite, parameters are bound in the order they appear in the query, regardless of the order in the function signature.
|
|
|
|
```go
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
type DBTX interface {
|
|
ExecContext(context.Context, string, ...any) (sql.Result, error)
|
|
}
|
|
|
|
func New(db DBTX) *Queries {
|
|
return &Queries{db: db}
|
|
}
|
|
|
|
type Queries struct {
|
|
db DBTX
|
|
}
|
|
|
|
const updateAuthor = `-- name: UpdateAuthor :exec
|
|
UPDATE authors SET bio = $2
|
|
WHERE id = $1
|
|
`
|
|
|
|
type UpdateAuthorParams struct {
|
|
ID int32
|
|
Bio string
|
|
}
|
|
|
|
func (q *Queries) UpdateAuthor(ctx context.Context, arg UpdateAuthorParams) error {
|
|
_, err := q.db.ExecContext(ctx, updateAuthor, arg.ID, arg.Bio)
|
|
return err
|
|
}
|
|
```
|
|
|