Last active
November 15, 2022 15:51
-
-
Save asspirin12/50ec831ffbd7c3b0019aece1836325c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
"fmt" | |
"github.com/go-sql-driver/mysql" | |
"log" | |
"os" | |
) | |
var db *sql.DB | |
type Album struct { | |
ID int64 | |
Title string | |
Artist string | |
Price float32 | |
Quantity int64 | |
} | |
func main() { | |
// Capture connection properties. | |
cfg := mysql.Config{ | |
User: os.Getenv("DBUSER"), | |
Passwd: os.Getenv("DBPASS"), | |
Net: "tcp", | |
Addr: "127.0.0.1:3306", | |
DBName: "recordings", | |
} | |
// Get a database handle. | |
var err error | |
db, err = sql.Open("mysql", cfg.FormatDSN()) | |
if err != nil { | |
log.Fatal(err) | |
} | |
pingErr := db.Ping() | |
if pingErr != nil { | |
log.Fatal(pingErr) | |
} | |
fmt.Println("Connected!") | |
// Inserting data | |
//albID, err := addAlbum(Album{ | |
// Title: "The Modern Sound of Betty Carter", | |
// Artist: "Betty Carter", | |
// Price: 49.99, | |
// Quantity: 10, | |
//}) | |
//if err != nil { | |
// log.Fatal(err) | |
//} | |
//fmt.Printf("ID of added album: %v\n", albID) | |
// Querying a single row. | |
//alb, err := albumByID(2) | |
//if err != nil { | |
// log.Fatal(err) | |
//} | |
//fmt.Printf("Album found: %v\n", alb) | |
// Querying Multiple Rows | |
albums, err := albumsByArtist("John Coltrane") | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("Albums found: %v\n", albums) | |
// Using Prepared Statements | |
// Working with Transactions | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
// addAlbum adds the specified album to the database, | |
// returning the album ID of the new entry | |
func addAlbum(alb Album) (int64, error) { | |
result, err := db.Exec("INSERT INTO album (title, artist, price, quantity) VALUES (?, ?, ?, ?)", alb.Title, alb.Artist, alb.Price, alb.Quantity) | |
if err != nil { | |
return 0, fmt.Errorf("addAlbum: %v", err) | |
} | |
id, err := result.LastInsertId() | |
if err != nil { | |
return 0, fmt.Errorf("addAlbum: %v", err) | |
} | |
return id, nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
// albumsByArtist queries for albums that have the specified artist name. | |
func albumsByArtist(name string) ([]Album, error) { | |
// An albums slice to hold data from returned rows. | |
var albums []Album | |
rows, err := db.Query("SELECT * FROM album WHERE artist = ?", name) | |
if err != nil { | |
return nil, fmt.Errorf("albumsByArtist %q: %v", name, err) | |
} | |
defer rows.Close() | |
// Loop through rows, using Scan to assign column data to struct fields. | |
for rows.Next() { | |
var alb Album | |
if err := rows.Scan(&alb.ID, &alb.Title, &alb.Artist, &alb.Price, &alb.Quantity); err != nil { | |
return nil, fmt.Errorf("albumsByArtist %q: %v", name, err) | |
} | |
albums = append(albums, alb) | |
} | |
if err := rows.Err(); err != nil { | |
return nil, fmt.Errorf("albumsByArtist %q: %v", name, err) | |
} | |
return albums, nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
"fmt" | |
) | |
// albumByID queries for the album with the specified ID. | |
func albumByID(id int64) (Album, error) { | |
// An album to hold data from the returned row. | |
var alb Album | |
row := db.QueryRow("SELECT * FROM album WHERE id = ?", id) | |
if err := row.Scan(&alb.ID, &alb.Title, &alb.Artist, &alb.Price, &alb.Quantity); err != nil { | |
if err == sql.ErrNoRows { | |
return alb, fmt.Errorf("albumsById %d: no such album", id) | |
} | |
return alb, fmt.Errorf("albumsById %d: %v", id, err) | |
} | |
return alb, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment