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 TradingAccountUUID, Name FROM TradingAccounts WHERE TradingAccountUUID = ?", "c2ef66a5-a545-413b-9312-79a53caadbc4") defer rows.Close() for rows.Next() { var ( TradingAccountUUID string Name string ) rows.Scan(&TradingAccountUUID, &Name) fmt.Printf("TradingAccountUUID = %s, Name = %s\n", TradingAccountUUID, 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 TradingAccountUUID, Name FROM TradingAccounts WHERE TradingAccountUUID = ?") defer stmt.Close() rows, _ := stmt.Query("c2ef66a5-a545-413b-9312-79a53caadbc4 1") defer rows.Close() for rows.Next() { var ( TradingAccountUUID string Name string ) rows1.Scan(&TradingAccountUUID, &Name) fmt.Printf("TradingAccountUUID = %s, Name = %s\n", TradingAccountUUID, Name) } rows, _ = stmt.Query("c2ef66a5-a545-413b-9312-79a53caadbc4 2") defer rows.Close() for rows.Next() { var ( TradingAccountUUID string Name string ) rows2.Scan(&TradingAccountUUID, &Name) fmt.Printf("TradingAccountUUID = %s, Name = %s\n", TradingAccountUUID, Name) }