赞
踩
dbconfigdatastr字符串:用户名:密码@tcp(ip:端口)/数据名
连接数据库的那个go文件一定要引用: _ "github.com/go-sql-driver/mysql"
- import (
- "database/sql"
- "fmt"
- _ "github.com/go-sql-driver/mysql"
- "log"
- "net/http"
- )
- /*
- CreateMySQLConn 用于创建数据库链接,新的方法用于数据库核心认证
- */
- func CreateMySQLConn(dbconfigdatastr string) (*sql.DB, error) {
- //建立 mysql
- //fmt.Println("dbconfigdatastr", dbconfigdatastr)
- //formatOracleConnStr(&dbconfigdatastr)
- sqldbdata, err := sql.Open("mysql", dbconfigdatastr)
- //sqldbdata, err := sql.Open("oci8", dbconfigdatastr)
- if err != nil {
- sbjlog.Debug("sql application pool:%s\n", err)
- defer sqldbdata.Close()
- return sqldbdata, err
- }
- //测试创建链接是否成功
- err = sqldbdata.Ping()
- dbName := strings.Split(dbconfigdatastr, "/")[1]
- if err != nil {
- sbjlog.Debug("%s DB Ping err : %s\n", dbName, err)
- defer sqldbdata.Close()
- } else {
- sbjlog.Printf("The Sql link is successful - %s.", dbName)
- }
- return sqldbdata, err
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。