golang连接MySQL并导入执行SQL文件

实现golang连接MySQL数据库导入执行一个sql文件

package tools

import (
    "fmt"
    _ "github.com/go-sql-driver/mysql"
    "github.com/jinzhu/gorm"
    "io/ioutil"
    "log"
    "os"
    "strings"
    "time"
)

type ImportSqlTool struct {
    SqlPath                                    string
    Username, Password, Server, Port, Database string
}

func (this *ImportSqlTool) ImportSql() error {
    _, err := os.Stat(this.SqlPath)
    if os.IsNotExist(err) {
        log.Println("数据库SQL文件不存在:", err)
        return err
    }

    dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", this.Username, this.Password, this.Server, this.Port, this.Database)
    db, err := gorm.Open("mysql", dsn)
    if err != nil {
        log.Println("数据库连接失败:", err)
        //panic("数据库连接失败!")
        return err
    }
    db.SingularTable(true)
    db.LogMode(true)
    db.DB().SetMaxIdleConns(10)
    db.DB().SetMaxOpenConns(100)
    db.DB().SetConnMaxLifetime(59 * time.Second)

    sqls, _ := ioutil.ReadFile(this.SqlPath)
    sqlArr := strings.Split(string(sqls), ";")
    for _, sql := range sqlArr {
        sql = strings.TrimSpace(sql)
        if sql == "" {
            continue
        }
        err := db.Exec(sql).Error
        if err != nil {
            log.Println("数据库导入失败:" + err.Error())
            return err
        } else {
            log.Println(sql, "\t success!")
        }
    }
    return nil
}

使用方法

    tool:=&ImportSqlTool{
        SqlPath:  "../import.sql",
        Username: "go-fly",
        Password: "go-fly",
        Server:   "127.0.0.1",
        Port:     "3306",
        Database: "go-fly",
    }
    tool.ImportSql()

 

posted @ 2022-11-09 22:21  唯一客服系统开发笔记  阅读(986)  评论(0编辑  收藏  举报