Reputation: 113
These two schemes can achieve the same function, so what`s the difference between them? reference: https://golang.org/pkg/database/sql/
Upvotes: 4
Views: 2705
Reputation: 16670
DB.Query
func (db *DB) Query(query string, args ...interface{}) (*Rows, error)
Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
By definition this accepts a single query and returns rows to iterate from
DB.Prepare() + Stmt.Query()
func (db *DB) Prepare(query string) (*Stmt, error)
Prepare creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement
func (s *Stmt) Query(args ...interface{}) (*Rows, error)
Query executes a prepared query statement with the given arguments and returns the query results as a *Rows.
Since DB.prepare can accept multiple queries and Stmt.Query can run them concurrently, this can be used to run multiple concurrent queries as well, unlike the DB.Query which is used to execute a single query
Upvotes: 3