Parameterized Statements
The following code example shows how to bind parameters to create parameterized statements.
Single-Use Statements
The Query and Exec functions both accept additional parameters for binding query parameters to values.
rows, _ := db.Query("SELECT VisibilityCode, Comment FROM CompanyStatusUpdates WHERE EntityId = ?", "238")
defer rows.Close()
for rows.Next() {
var (
VisibilityCode string
Comment string
)
rows.Scan(&VisibilityCode, &Comment)
fmt.Printf("VisibilityCode = %s, Comment = %s\n", VisibilityCode, Comment)
}
Reusable Statements
The Prepare function creates prepared Stmt objects, which can be re-used across multiple Query and Exec calls.
stmt, _ := db.Prepare("SELECT VisibilityCode, Comment FROM CompanyStatusUpdates WHERE EntityId = ?")
defer stmt.Close()
rows, _ := stmt.Query("238 1")
defer rows.Close()
for rows.Next() {
var (
VisibilityCode string
Comment string
)
rows1.Scan(&VisibilityCode, &Comment)
fmt.Printf("VisibilityCode = %s, Comment = %s\n", VisibilityCode, Comment)
}
rows, _ = stmt.Query("238 2")
defer rows.Close()
for rows.Next() {
var (
VisibilityCode string
Comment string
)
rows2.Scan(&VisibilityCode, &Comment)
fmt.Printf("VisibilityCode = %s, Comment = %s\n", VisibilityCode, Comment)
}