0% found this document useful (0 votes)
2 views1 page

database-sql

This document provides a quick reference guide for using the database/sql package in Go, detailing how to fetch multiple rows, create a connection pool, execute SQL statements, and manage transactions. It includes code snippets for querying data, handling errors, and using placeholders in SQL commands. Additionally, it highlights specific considerations for PostgreSQL, such as the use of the RETURNING clause and $N notation for parameters.

Uploaded by

joaovmmachado
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

database-sql

This document provides a quick reference guide for using the database/sql package in Go, detailing how to fetch multiple rows, create a connection pool, execute SQL statements, and manage transactions. It includes code snippets for querying data, handling errors, and using placeholders in SQL commands. Additionally, it highlights specific considerations for PostgreSQL, such as the use of the RETURNING clause and $N notation for parameters.

Uploaded by

joaovmmachado
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

db.

Query: Fetch multiple rows


GO QUICK REFERENCE GUIDE database/sql
rows, err := db.Query("SELECT name FROM customers LIMIT 10")
if err != nil {
sql.Open: Create a sql.DB connection pool
log.Fatal(err)
}
db, err := sql.Open("DRIVER_NAME", "CONNECTION_STRING")
defer rows.Close()
if err != nil {
log.Fatal(err)
names := []string{}
}
for rows.Next() {
defer db.Close()
var name string
// Use Ping() to create a connection and check for any errors.
err := rows.Scan(&name)
if err = db.Ping(); err != nil {
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
}
names = append(names, name)
}
db.Exec: Execute a SQL statement

if err = rows.Err(); err != nil {


result, err := db.Exec("INSERT INTO customers (name) VALUES (?)", "Alice")
log.Fatal(err)
if err != nil {
}
log.Fatal(err)
}
// Note: The LastInsertID() method is not supported by PostgreSQL. Use the
// db.QueryRow() method with a "RETURNING" clause instead. sql.Tx: Execute multiple statements in one transaction
id, err := result.LastInsertId()
if err != nil { // All statements in a transaction use the same database connection. Either all
log.Fatal(err) // statements are executed successfully, or none at all.
} tx, err := db.Begin()
affected, err := result.RowsAffected() if err != nil {
if err != nil { log.Fatal(err)
log.Fatal(err) }
}
_, err := tx.Exec("INSERT INTO customers ...")
if err != nil {
db.QueryRow: Fetch a single row tx.Rollback()
log.Fatal(err)
var name string }
err := db.QueryRow("SELECT name FROM customers WHERE id = ?", 1).Scan(&name)
if err == sql.ErrNoRows { // ...
log.Fatal("no rows returned")
} else if err != nil { err = tx.Commit()
log.Fatal(err)
}
PostgreSQL: Use $N notation for placeholder parameters instead of the ? character.

You might also like