From 6f08b9faf2f92752b3246cf52a1f3be76c3882a9 Mon Sep 17 00:00:00 2001 From: Kevin Burke Date: Tue, 30 Jan 2018 19:25:37 -0800 Subject: 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 Run-TryBot: Daniel Theophanes TryBot-Result: Gobot Gobot --- src/database/sql/sql.go | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) (limited to 'src/database/sql/sql.go') 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) -- cgit v1.3