blog_backend_api/db/sqlManager.go

30 lines
554 B
Go

package db
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"user/config"
)
var gDb Database
func Init() {
mysqlconf := config.GetMysqlConfig()
cnn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", mysqlconf.UserName, mysqlconf.Password,
mysqlconf.Addr, mysqlconf.Db)
_db, err := sql.Open("mysql", cnn)
if err != nil {
fmt.Println("connect sql server ", err.Error())
}
e := _db.Ping()
if nil != e {
fmt.Println(e.Error())
}
gDb = Database{Type: string(""), DB: _db}
}
func GetMysqlClient() *Database {
return &gDb
}