Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the quickstart example so it actually works #532

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,31 @@ func main() {
CREATE TABLE user (
id INT PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL
age INT NOT NULL,
created_at TIMESTAMP
)
`)

err = db.Exec(`INSERT INTO user (id, name, age) VALUES ($1, $2, $3)`, 20, "foo", 40)
err = db.Exec(`INSERT INTO user (id, name, age) VALUES (1, "Jo Bloggs", 33)`)

rows, err := db.Query("SELECT id, name, age, address FROM user WHERE age >= $1", 18)
rows, err := db.Query("SELECT id, name, age, address FROM user WHERE age >= 18")
defer rows.Close()

err = rows.Iterate(func(r *chai.Row) error {
// scan each column
var id, name, age
var id, age int
var name string
err = r.Scan(&id, &name, &age)
// or into a struct
type User struct {
ID int
Name string
Age int
}
var u User
err = r.StructScan(&u)
// or even a map
var m map[string]any
m := make(map[string]any)
err = r.MapScan(&m)
return nil
})
Expand Down