Skip to content

Instantly share code, notes, and snippets.

@xaprb
Created January 8, 2015 12:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xaprb/dcd83a45c6beed2d6bb7 to your computer and use it in GitHub Desktop.
Save xaprb/dcd83a45c6beed2d6bb7 to your computer and use it in GitHub Desktop.
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
Copy link

_ "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