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