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