mirror of https://github.com/jackc/pgx.git
Improved example on readme
parent
da16226e0a
commit
a7e821c99c
36
README.md
36
README.md
|
@ -8,17 +8,33 @@ pgx is a pure Go driver and toolkit for PostgreSQL. It is usable through databas
|
||||||
## Example Usage
|
## Example Usage
|
||||||
|
|
||||||
```go
|
```go
|
||||||
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
|
package main
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Unable to connection to database: %v\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
var name string
|
import (
|
||||||
var weight int64
|
"context"
|
||||||
err := conn.QueryRow(context.Background(), "select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
|
"fmt"
|
||||||
if err != nil {
|
"os"
|
||||||
return err
|
|
||||||
|
"github.com/jackc/pgx/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Unable to connection to database: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer conn.Close(context.Background())
|
||||||
|
|
||||||
|
var name string
|
||||||
|
var weight int64
|
||||||
|
err = conn.QueryRow(context.Background(), "select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(name, weight)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue