background/db/sqlManager.go

30 lines
554 B
Go
Raw Normal View History

2019-01-25 09:11:15 +00:00
package db
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
2019-01-29 13:57:53 +00:00
"user/config"
2019-01-25 09:11:15 +00:00
)
var gDb Database
2019-03-07 02:36:09 +00:00
func Init() {
2019-01-25 09:11:15 +00:00
mysqlconf := config.GetMysqlConfig()
2019-03-07 02:36:09 +00:00
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())
2019-01-25 09:11:15 +00:00
}
e := _db.Ping()
2019-03-07 02:36:09 +00:00
if nil != e {
2019-01-25 09:11:15 +00:00
fmt.Println(e.Error())
}
2019-03-07 02:36:09 +00:00
gDb = Database{Type: string(""), DB: _db}
2019-01-25 09:11:15 +00:00
}
func GetMysqlClient() *Database {
return &gDb
}