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