Golang database/sql.Rows.Next function examples
package database/sql
Next prepares the next result row for reading with the Scan method. It returns true on success, or false if there is no next result row or an error happened while preparing it. Err should be consulted to distinguish between the two cases.
Every call to Scan, even the first one, must be preceded by a call to Next.
Golang database/sql.Rows.Next function usage examples
Example 1:
rows, err := db.Query("INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets') RETURNING did;")
if err != nil {
t.Fatal(err)
}
if !rows.Next() { // <----- here
t.Fatal("no rows")
}
Example 2:
rows, err := db.conn.Query("SELECT name, parent_id FROM edge WHERE entity_id = ?;", id)
if err != nil {
return refs
}
defer rows.Close()
for rows.Next() { // <--- here
var name string
var parentId string
if err := rows.Scan(&name, &parentId); err != nil {
return refs
}
refs = append(refs, &Edge{
EntityID: id,
Name: name,
ParentID: parentId,
})
}
References :
https://github.com/docker/docker/blob/master/pkg/graphdb/graphdb.go
Advertisement
Something interesting
Tutorials
+14.5k Golang : How to determine if user agent is a mobile device example
+12.4k Golang : Extract part of string with regular expression
+12.7k Golang : Remove or trim extra comma from CSV
+8.9k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+4.3k Javascript : How to show different content with noscript?
+20.7k Golang : Saving private and public key to files
+17.8k Golang : Defer function inside init()
+12.6k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+9k Golang : Populate or initialize struct with values example
+4k Detect if Google Analytics and Developer Media are loaded properly or not
+21.7k Golang : Setting up/configure AWS credentials with official aws-sdk-go