go语言查询mysql数据库

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

package dal

import (
	"encoding/json"
	"fmt"
	"strconv"
	"strings"

	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

type automethod struct {
	ActionId      int    `orm:"pk;auto;column(ActionId)"`
	ActionName    string `orm:"size(150);column(ActionName)"`
	ActionArg     string `orm:"size(2000);column(ActionArg)"`
	ActionType    int    `orm:"size(11);column(ActionType)"`
	MethodRunName string `orm:"size(150);column(MethodRunName)"`
	AssemblyId    int    `orm:"size(11);column(AssemblyId)"`
	ClassName     string `orm:"size(150);column(ClassName)"`
	SubActionId   int    `orm:"size(11);column(SubActionId)"`
	ActionRuntime string `orm:"size(150);column(ActionRuntime)"`
}

var linkErrMsg string
var o orm.Ormer

func init() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("recover success")
		}
	}()
	orm.RegisterModelWithPrefix("", new(automethod))
	ErrorHandle(orm.RegisterDriver("mysql", orm.DRMySQL))
	ErrorHandle(orm.RegisterDataBase("default", "mysql", "root:123456@tcp(10.101.42.38:3306)/mytest?charset=utf8"))
	CheckIsConn(linkErrMsg)
}

func ErrorHandle(err error) {
	if err != nil {
		linkErrMsg = "RegisterDataBase_Error:" + err.Error()
		panic(linkErrMsg)
	}
}

func CheckIsConn(b string) {
	if b != "" {
		panic(b)
	} else {
		o = orm.NewOrm()
		o.Using("mytest")
	}
}

func PrintActionJson() interface{} {
	var response string
	if linkErrMsg != "" {
		return linkErrMsg
	}
	var actionInfos []*automethod
	sel := o.QueryTable("automethod")
	count, err := sel.All(&actionInfos)
	if err != nil {
		response = "条数" + string(count) + err.Error()
	}
	//json转换
	jsonData, err := json.Marshal(actionInfos)
	if err != nil {
		response = err.Error()
	}
	newdata := make([]automethod, len(actionInfos))
	json.Unmarshal(jsonData, &newdata)
	response = string(jsonData)
	return response
}

func PrintActionModel() interface{} {
	var response string
	if linkErrMsg != "" {
		return linkErrMsg
	}
	var actionInfos []*automethod
	sel := o.QueryTable("automethod")
	count, err := sel.All(&actionInfos)
	if err != nil {
		response = "条数" + string(count) + err.Error()
	}
	//json转换
	jsonData, err := json.Marshal(actionInfos)
	if err != nil {
		response = err.Error()
	}
	newdata := make([]automethod, len(actionInfos))
	json.Unmarshal(jsonData, &newdata)
	response = string(jsonData)
	return response
}

func returnString(s string) string {
	return s
}

func PrintActionIdAndName() interface{} {
	var response string
	if linkErrMsg != "" {
		return linkErrMsg
	}
	var actionIds []int
	var actionNames []string

	raw := o.Raw("select ActionId,ActionName from automethod")
	num, err := raw.QueryRows(&actionIds, &actionNames)
	var rowsData = make([]string, num, 100)
	if err == nil {
		for i := int64(0); i < num-1; i++ {
			rowsData[i] = strconv.Itoa(actionIds[i]) + actionNames[i]
		}
		response = strings.Join(rowsData, ",")
	} else {
		response = err.Error()
	}
	return response
}

 

转载于:https://my.oschina.net/u/252343/blog/830021

你可能感兴趣的:(go语言查询mysql数据库)