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