diff options
| author | Kevin Burke <kev@inburke.com> | 2018-01-30 19:25:37 -0800 |
|---|---|---|
| committer | Daniel Theophanes <kardianos@gmail.com> | 2018-03-25 05:09:57 +0000 |
| commit | 6f08b9faf2f92752b3246cf52a1f3be76c3882a9 (patch) | |
| tree | a429ecd5fefb523f1b4b6cf43d870573fe329b0b /src/database/sql/sql.go | |
| parent | 782f9ce52f5e021a8d0b0140919afecf0733c4a7 (diff) | |
| download | go-6f08b9faf2f92752b3246cf52a1f3be76c3882a9.tar.xz | |
database/sql: add more examples
This aims to expand the coverage of examples showing how the sql
package works, as well as to address a number of issues I've observed
while explaining how the database package works:
- The best way to issue UPDATE or INSERT queries, that don't need
to scan anything in return. (Previously, we had no examples for any
Execute statement).
- How to use prepared statements and transactions.
- How to aggregate arguments from a Query/QueryContext query into
a slice.
Furthermore just having examples in more places should help, as users
click on e.g. the "Rows" return parameter and are treated with the
lack of any example about how Rows is used.
Switch package examples to use QueryContext/QueryRowContext; I think
it is a good practice to prepare users to issue queries with a timeout
attached, even if they are not using it immediately.
Change-Id: I4e63af91c7e4fff88b25f820906104ecefde4cc3
Reviewed-on: https://go-review.googlesource.com/91015
Reviewed-by: Daniel Theophanes <kardianos@gmail.com>
Run-TryBot: Daniel Theophanes <kardianos@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/database/sql/sql.go')
| -rw-r--r-- | src/database/sql/sql.go | 19 |
1 files changed, 1 insertions, 18 deletions
diff --git a/src/database/sql/sql.go b/src/database/sql/sql.go index 05d15455c0..784ffac26d 100644 --- a/src/database/sql/sql.go +++ b/src/database/sql/sql.go @@ -2472,11 +2472,6 @@ func rowsiFromStatement(ctx context.Context, ci driver.Conn, ds *driverStmt, arg // If the query selects no rows, the *Row's Scan will return ErrNoRows. // Otherwise, the *Row's Scan scans the first selected row and discards // the rest. -// -// Example usage: -// -// var name string -// err := nameByUseridStmt.QueryRowContext(ctx, id).Scan(&name) func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *Row { rows, err := s.QueryContext(ctx, args...) if err != nil { @@ -2545,19 +2540,7 @@ func (s *Stmt) finalClose() error { } // Rows is the result of a query. Its cursor starts before the first row -// of the result set. Use Next to advance through the rows: -// -// rows, err := db.Query("SELECT ...") -// ... -// defer rows.Close() -// for rows.Next() { -// var id int -// var name string -// err = rows.Scan(&id, &name) -// ... -// } -// err = rows.Err() // get any error encountered during iteration -// ... +// of the result set. Use Next to advance from row to row. type Rows struct { dc *driverConn // owned; must call releaseConn when closed to release releaseConn func(error) |
