Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "database/sql" | |
| "database/sql/driver" | |
| "fmt" | |
| "net" | |
| "os" | |
| "time" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var ids := pq.Int64Array{} | |
| err := db.QueryRow(`SELECT array_agg(id) FROM users WHERE "age" > $1`,18).Scan(&ids) | |
| … |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ids := []int64{12,16,33,55} | |
| rows,err := db.Query(`SELECT name FROM users WHERE id=ANY($1),pq.Int64Array(ids)) | |
| … |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var count int | |
| err := db.QueryRow(`SELECT count(*) FROM users`).Scan(&count) | |
| if err != nil { | |
| if err == sql.ErrNoRaw { | |
| count = 0 | |
| … | |
| } | |
| … | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| err := db.Exec(`INSERT INTO users(name,favorite_fruit,age) | |
| VALUES($1, $2, $3) | |
| ON CONFLICT (name) DO UPDATE | |
| SET favorite_fruit=$2, | |
| age=$3`) | |
| err := db.Exec(`INSERT INTO users(name,favorite_fruit,age) | |
| VALUES($1, $2, $3) | |
| ON CONFLICT (name) DO UPDATE | |
| SET favorite_fruit=$2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var userid int | |
| err := db.QueryRow(`INSERT INTO users(name, favorite_fruit, age) | |
| VALUES('beatrice', 'starfruit', 93) RETURNING id`).Scan(&userid) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // $2 使用了两次 都标记的值是64 | |
| rows, err := db.Query(`SELECT name FROM users WHERE favorite_fruit = $1 | |
| OR age BETWEEN $2 AND $2 + 3`, "orange", 64) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "database/sql" | |
| "net/http" | |
| "time" | |
| _ "github.com/mattn/go-sqlite3" | |
| ) |