username := os.Getenv("DB_USER")
password := os.Getenv("DB_PASS")
// Specify connection properties.
cfg := mysql.Config{
User: username,
Passwd: password,
Net: "tcp",
Addr: "127.0.0.1:3306",
DBName: "jazzrecords",
}
// Get a driver-specific connector.
connector, err := mysql.NewConnector(&cfg)
if err != nil {
log.Fatal(err)
}
// Get a database handle.
db = sql.OpenDB(connector)
// Specify connection properties.
cfg := mysql.Config{
User: username,
Passwd: password,
Net: "tcp",
Addr: "127.0.0.1:3306",
DBName: "jazzrecords",
}
// Get a database handle.
db, err = sql.Open("mysql", cfg.FormatDSN())
if err != nil {
log.Fatal(err)
}
import "github.com/go-sql-driver/mysql"
db, err = sql.Open("mysql", connString)
// Confirm a successful connection.
if err := db.Ping(); err != nil {
log.Fatal(err)
}
import _ "github.com/go-sql-driver/mysql"
username := os.Getenv("DB_USER")
password := os.Getenv("DB_PASS")
db, err = sql.Open("mysql", "username:password@tcp(127.0.0.1:3306)/jazzrecords")
if err != nil {
log.Fatal(err)
}
rows, err := db.Query("SELECT * FROM album WHERE artist = ?", artist)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// Loop through returned rows.
db.onerror = event => {
// Generic error handler for all errors targeted at this database's
// requests!
console.error("Database error: " + event.target.errorCode);
};
var db *sql.DB
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!")
}
Recommend
Go Opening a database handle Confirming a connection
Go Opening a database handle Opening a database handle Opening with a Connector
Go Opening a database handle Opening a database handle Opening with a connection string
Go Opening a database handle Locating and importing a database driver
Go Call your code from another module
Go Compile and install the application
Go Return greetings for multiple people
Tutorial: Get started with Go Call code in an external package
Tutorial: Get started with Go Write some code
Go Tutorial: Getting started with fuzzing Completed code
Go Tutorial: Getting started with fuzzing Fix the double reverse error Fix the error Run the code
Go Tutorial: Getting started with fuzzing Fix the double reverse error Fix the error Write the code
Go Tutorial: Getting started with fuzzing Fix the invalid string error Fix the error Run the code
Go Tutorial: Getting started with fuzzing Fix the invalid string error Fix the error Write the code
Go Tutorial: Getting started with fuzzing Fix the invalid string error Diagnose the error
Go Tutorial: Getting started with fuzzing Add a fuzz test Run the code
Go Tutorial: Getting started with fuzzing Add a fuzz test Write the code
Go Tutorial: Getting started with fuzzing Add a unit test Run the code
Go Tutorial: Getting started with fuzzing Add a unit test Write the code
Go Tutorial: Getting started with fuzzing Add code to test Run the code
Go Tutorial: Getting started with fuzzing Add code to test Write the code
Go Tutorial: Getting started with fuzzing Create a folder for your code
Tutorial: Create a Go module Start a module that others can use
Go Tutorial: Getting started with multi-module workspaces Create a module for your code