dbconn.query golang sql example
Sure! Here's an example of using the query
method in the database/sql
package in Go:
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
)
func main() {
// Open a connection to the database
db, err := sql.Open("mysql", "username:password@tcp(localhost:3306)/database")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Execute a query
rows, err := db.Query("SELECT id, name, age FROM users")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// Iterate over the result set
for rows.Next() {
var id int
var name string
var age int
if err := rows.Scan(&id, &name, &age); err != nil {
log.Fatal(err)
}
fmt.Println("ID:", id, "Name:", name, "Age:", age)
}
// Check for any errors during iteration
if err := rows.Err(); err != nil {
log.Fatal(err)
}
}
In this example, we first open a connection to the database using the sql.Open
function, specifying the driver and connection string. Then, we use the db.Query
method to execute the SQL query and obtain a *sql.Rows
result set.
We iterate over the result set using the rows.Next
method and retrieve the values using the rows.Scan
method. Finally, we print the values and check for any errors during iteration using rows.Err
.
Note that you need to import the appropriate database driver package, in this case github.com/go-sql-driver/mysql
, and replace the connection string with your own MySQL database details.
query with param
To execute a query with parameters in Go using the database/sql
package, you can use the Query
method along with placeholders for the parameters. Here's an example:
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
)
func main() {
// Open a connection to the database
db, err := sql.Open("mysql", "username:password@tcp(localhost:3306)/database")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Prepare the SQL statement with placeholders
stmt, err := db.Prepare("SELECT id, name, age FROM users WHERE age > ?")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
// Execute the query with the parameter value
rows, err := stmt.Query(18)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// Iterate over the result set
for rows.Next() {
var id int
var name string
var age int
if err := rows.Scan(&id, &name, &age); err != nil {
log.Fatal(err)
}
fmt.Println("ID:", id, "Name:", name, "Age:", age)
}
// Check for any errors during iteration
if err := rows.Err(); err != nil {
log.Fatal(err)
}
}
In this example, we first open a connection to the database using sql.Open
and then prepare the SQL statement using db.Prepare
, specifying the SQL query with a placeholder ?
for the parameter.
We then execute the query using stmt.Query
, passing the parameter value (in this case, 18
). The result set is iterated over in the same way as before, using rows.Next
and rows.Scan
.
Note that you need to import the appropriate database driver package, in this case github.com/go-sql-driver/mysql
, and replace the connection string with your own MySQL database details.
use $1
If you want to use positional parameters in your SQL query instead of placeholders, you can modify the example as follows:
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
)
func main() {
// Open a connection to the database
db, err := sql.Open("mysql", "username:password@tcp(localhost:3306)/database")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Prepare the SQL statement with positional parameter
stmt, err := db.Prepare("SELECT id, name, age FROM users WHERE age > ?")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
// Execute the query with the positional parameter value
rows, err := stmt.Query(18)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// Iterate over the result set
for rows.Next() {
var id int
var name string
var age int
if err := rows.Scan(&id, &name, &age); err != nil {
log.Fatal(err)
}
fmt.Println("ID:", id, "Name:", name, "Age:", age)
}
// Check for any errors during iteration
if err := rows.Err(); err != nil {
log.Fatal(err)
}
}
In this modified example, we use the ?
placeholder in the SQL query. When preparing the statement, we don't need to specify the positional parameter explicitly. Instead, we directly pass the parameter value (in this case, 18
) to stmt.Query
.
Note that the behavior of placeholders and positional parameters may vary depending on the database driver you are using. The example provided assumes the MySQL driver, but other drivers may use different syntax or conventions for parameters.