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