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