new exporter version
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
# Getting Started with The database/sql Package
|
||||
|
||||
This hands-on tutorial will show you how to get started with the database/sql package.
|
||||
|
||||
Find the tutorial [here](https://blog.jetbrains.com/go/2023/02/28/getting-started-with-the-database-sql-package/).
|
||||
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
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!")
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
module database-sql-package-goproject
|
||||
|
||||
go 1.19
|
||||
|
||||
require github.com/go-sql-driver/mysql v1.7.0 // indirect
|
||||
@@ -0,0 +1,2 @@
|
||||
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
|
||||
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||
@@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
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!")
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
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!")
|
||||
|
||||
albums, err := albumsByArtist("John Coltrane")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Albums found: %v\n", albums)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
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!")
|
||||
|
||||
// Hard-code ID 2 here to test the query.
|
||||
Album, err := albumByID(2)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Album found: %v\n", Album)
|
||||
}
|
||||
|
||||
// AlbumByID retrieves the specified album.
|
||||
func albumByID(id int) (Album, error) {
|
||||
// Define a prepared statement. You'd typically define the statement
|
||||
// elsewhere and save it for use in functions such as this one.
|
||||
stmt, err := db.Prepare("SELECT * FROM album WHERE id = ?")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var album Album
|
||||
|
||||
// Execute the prepared statement, passing in an id value for the
|
||||
// parameter whose placeholder is ?
|
||||
err = stmt.QueryRow(id).Scan(&album.ID, &album.Title, &album.Artist, &album.Price, &album.Quantity)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
// Handle the case of no rows returned.
|
||||
}
|
||||
return album, err
|
||||
}
|
||||
return album, nil
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
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!")
|
||||
|
||||
// Hard-code ID 2 here to test the query.
|
||||
alb, err := albumByID(2)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Album found: %v\n", alb)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
DROP TABLE IF EXISTS album;
|
||||
CREATE TABLE album (
|
||||
id INT AUTO_INCREMENT NOT NULL,
|
||||
title VARCHAR(128) NOT NULL,
|
||||
artist VARCHAR(255) NOT NULL,
|
||||
price DECIMAL(5,2) NOT NULL,
|
||||
quantity INT UNSIGNED,
|
||||
PRIMARY KEY (`id`)
|
||||
);
|
||||
|
||||
INSERT INTO album
|
||||
(title, artist, price, quantity)
|
||||
VALUES
|
||||
('Blue Train', 'John Coltrane', 56.99, 5),
|
||||
('Giant Steps', 'John Coltrane', 63.99, 62),
|
||||
('Jeru', 'Gerry Mulligan', 17.99, 0),
|
||||
('Sarah Vaughan', 'Sarah Vaughan', 34.98, 127);
|
||||
@@ -0,0 +1,6 @@
|
||||
DROP TABLE IF EXISTS album_trx;
|
||||
CREATE TABLE album_trx (
|
||||
trx_id INT AUTO_INCREMENT NOT NULL,
|
||||
trx_check INT UNSIGNED,
|
||||
PRIMARY KEY (`trx_id`)
|
||||
);
|
||||
@@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
var db *sql.DB
|
||||
|
||||
type Album struct {
|
||||
ID int64
|
||||
Title string
|
||||
Artist string
|
||||
Price float32
|
||||
Quantity int64
|
||||
}
|
||||
|
||||
type Album_trx struct {
|
||||
TRX_ID int64
|
||||
TRX_CHECK 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!")
|
||||
|
||||
// Start the transaction
|
||||
ctx := context.Background()
|
||||
tx, err := db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// First query
|
||||
_, err = tx.ExecContext(ctx, "INSERT INTO album (title, artist, price, quantity) VALUES ('Master of Puppets', 'Metallica', '49', '1')")
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
|
||||
// Second query
|
||||
_, err = tx.ExecContext(ctx, "INSERT INTO album_trx (trx_check) VALUES (1)")
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
fmt.Println("Transaction declined")
|
||||
return
|
||||
}
|
||||
|
||||
// If no errors, commit the transaction
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("Transaction accepted!")
|
||||
}
|
||||
Reference in New Issue
Block a user