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