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