package main
import (
"context"
"fmt"
"github.com/jackc/pgx/v4"
"log"
)
func main() {
// Create a connection to the database
conn, err := pgx.Connect(context.Background(), "postgres://username:password@localhost:5432/mydatabase")
if err != nil {
log.Fatal("Unable to connect to database:", err)
}
defer conn.Close(context.Background())
// Execute a SQL query
rows, err := conn.Query(context.Background(), "SELECT id, name FROM users")
if err != nil {
log.Fatal("Query failed:", err)
}
defer rows.Close()
// Scan the rows
for rows.Next() {
var id int
var name string
err := rows.Scan(&id, &name)
if err != nil {
log.Fatal("Row scan failed:", err)
}
fmt.Printf("ID: %d, Name: %s\n", id, name)
}
// Check for any errors encountered during iteration
if err := rows.Err(); err != nil {
log.Fatal("Row iteration error:", err)
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?