You're developing a web application in Go, and you need to execute a SQL query that might return multiple rows. Which method from the database/sql package would you use, and how would you handle the returned rows?
- Exec method; Execute the query and retrieve the result set directly. Use rows.Scan() within a loop to scan each row into variables for processing.
- Prepare method; Prepare the SQL statement, execute it using Query or QueryRow, then scan each row using rows.Scan().
- Query method; Use rows.Next() in a loop to fetch each row one by one, then process each row's data.
- QueryRows method; Iterate over the rows using a loop and scan each row into a struct to process the data.
The correct method to use in this scenario is QueryRows. This method allows executing a SQL query that returns multiple rows. You can iterate over the rows using a loop and scan each row into a struct or variables for further processing.
Loading...
Related Quiz
- In Go, errors are returned as the _____ value in functions.
- What is the main difference between authentication and authorization?
- The _______ interface in the database/sql package is used to represent a prepared statement.
- In Go, what is the role of the 'panic' function in error handling?
- What percentage of code coverage is typically considered acceptable in many software development projects?