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 AssociateOID, WorkerID FROM Workers WHERE AssociateOID = ?", "G3349PZGBADQY8H8") defer rows.Close() for rows.Next() { var ( AssociateOID string WorkerID string ) rows.Scan(&AssociateOID, &WorkerID) fmt.Printf("AssociateOID = %s, WorkerID = %s\n", AssociateOID, WorkerID) }
Reusable Statements
The Prepare function creates prepared Stmt objects, which can be re-used across multiple Query and Exec calls.
stmt, _ := db.Prepare("SELECT AssociateOID, WorkerID FROM Workers WHERE AssociateOID = ?") defer stmt.Close() rows, _ := stmt.Query("G3349PZGBADQY8H8 1") defer rows.Close() for rows.Next() { var ( AssociateOID string WorkerID string ) rows1.Scan(&AssociateOID, &WorkerID) fmt.Printf("AssociateOID = %s, WorkerID = %s\n", AssociateOID, WorkerID) } rows, _ = stmt.Query("G3349PZGBADQY8H8 2") defer rows.Close() for rows.Next() { var ( AssociateOID string WorkerID string ) rows2.Scan(&AssociateOID, &WorkerID) fmt.Printf("AssociateOID = %s, WorkerID = %s\n", AssociateOID, WorkerID) }