db.go 308 B

1234567891011121314151617
  1. package util
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "sync-block/er"
  6. _ "github.com/go-sql-driver/mysql"
  7. )
  8. func ConnectDb(host, id, pw, dbName string) *sql.DB {
  9. db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true",
  10. id, pw, host, dbName))
  11. er.Check(err)
  12. er.Check(db.Ping())
  13. return db
  14. }