Junior — Middle
What does using data protection mechanisms when forming SQL queries mean and what are their advantages?
sobes.tech AI
Answer from AI
Using data protection mechanisms when forming SQL queries in Go typically involves the use of prepared statements and parameterized queries.
Advantages:
- Protection against SQL injection: parameters are passed separately from the SQL code, preventing malicious commands from being injected.
- Improved performance: prepared queries are compiled once and can be executed multiple times with different parameters.
- Convenience and readability: the code becomes cleaner, making it easier to separate query logic from data.
Example of using a prepared statement in Go:
stmt, err := db.Prepare("SELECT * FROM users WHERE id = ?")
if err != nil {
// handle error
}
row := stmt.QueryRow(userID)
// further result processing