Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
hello_mysql.go
package main
import (
"database/sql"
"log"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql", "root:@tcp(:3306)/test")
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec(
"CREATE TABLE IF NOT EXISTS test.hello(world varchar(50))")
if err != nil {
log.Fatal(err)
}
res, err := db.Exec(
"INSERT INTO test.hello(world) VALUES('hello world!')")
if err != nil {
log.Fatal(err)
}
rowCount, err := res.RowsAffected()
if err != nil {
log.Fatal(err)
}
log.Printf("inserted %d rows", rowCount)
rows, err := db.Query("SELECT * FROM test.hello")
if err != nil {
log.Fatal(err)
}
for rows.Next() {
var s string
err = rows.Scan(&s)
if err != nil {
log.Fatal(err)
}
log.Printf("found row containing %q", s)
}
rows.Close()
}
@paulhay88

This comment has been minimized.

Copy link

paulhay88 commented Aug 25, 2018

_ "github.com/go-sql-driver/mysql"

PS G:\Programming> go run .\testDB.go
testDB.go:8:2: cannot find package "github.com/go-sql-driver/mysql" in any of:
C:\Go\src\github.com\go-sql-driver\mysql (from $GOROOT)
C:\Users\paul\go\src\github.com\go-sql-driver\mysql (from $GOPATH)

I've downloaded everything I need to I've followed the instructions in the book and i get nothing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.