db insert

This commit is contained in:
pengtao 2020-10-13 14:48:24 +08:00
parent 1f68bd94f3
commit e5d890a328
3 changed files with 138 additions and 35 deletions

View File

@ -10,18 +10,19 @@ import (
)
type ServiceList struct {
Id int32 `service id`
Name string `service name`
Host string `hostip`
area string `area`
port int `port`
Url string `monitor url`
user string `user`
sendto string `alter message send to`
Id int32 `json:"id"`
Name string `json:"name"`
Host string `json:"host"`
Area string `json:"area"`
Port int32 `json:"port"`
Url string `json:"url"`
User string `json:"user"`
Sendto string `json:"sendto"`
}
func GetServiceList(area string) (ss []ServiceList) {
func GetServiceList(area string) ([]ServiceList, error) {
var myconn config.MysqlConf
var v []ServiceList
myconn.GetMysqlConn()
path := strings.Join([]string{myconn.User, ":", myconn.Passwd, "@tcp(", myconn.Conn, ")/", "services", "?charset=utf8"}, "")
@ -36,28 +37,36 @@ func GetServiceList(area string) (ss []ServiceList) {
//验证连接
if err := db.Ping(); err != nil {
log.Print("opon database fail")
return
return v, err
}
rows, err := db.Query("select id,name,host,url FROM services.services_list where area=?", area)
rows, err := db.Query("select id,name,host,area,port,url,user,sendto FROM services.services_list where area=?", area)
//获取完毕释放rows阻止更多的列举
defer rows.Close()
if err != nil {
log.Println("获取错误:", err)
return
return v, err
}
//如果有数据记录Next指针就不为true
var v []ServiceList
for rows.Next() {
var id int32
var name string
var host string
var area string
var port int32
var url string
rows.Scan(&id, &name, &host, &url)
var user string
var sendto string
rows.Scan(&id, &name, &host, &area, &port, &url, &user, &sendto)
var s ServiceList
s.Id = id
s.Name = name
s.Host = host
s.Url = url
s.Port = port
s.Area = area
s.User = user
s.Sendto = sendto
v = append(v, s)
//log.Println(s)
}
@ -65,9 +74,9 @@ func GetServiceList(area string) (ss []ServiceList) {
err = rows.Err()
if err != nil {
log.Println("other error:", err)
return v
return v, err
}
//log.Println("return list with area", v)
return v
return v, nil
}

View File

@ -0,0 +1,75 @@
package mmysql
import (
"../config"
"database/sql"
"fmt"
"log"
"strings"
//导入mysql的驱动
_ "github.com/go-sql-driver/mysql"
)
type ServiceStatus struct {
Id int32 `json:"id"`
Cost int64 `json:"cost"`
Status bool `json:"status"`
Ip string `json:"ip"`
Area string `json:"area"`
}
func GetServiceStatus(c *ServiceStatus) bool {
log.Println("get message from client", c)
return true
}
func clearTransaction(tx *sql.Tx) {
err := tx.Rollback()
if err != sql.ErrTxDone && err != nil {
log.Fatalln(err)
}
}
func SaveServiceStatus(s []ServiceStatus) {
var myconn config.MysqlConf
myconn.GetMysqlConn()
path := strings.Join([]string{myconn.User, ":", myconn.Passwd, "@tcp(", myconn.Conn, ")/", "services", "?charset=utf8"}, "")
//打开数据库,前者是驱动名,所以要导入: _ "github.com/go-sql-driver/mysql"
db, err := sql.Open("mysql", path)
//tx, err := db.Begin()
if err != nil {
log.Fatalln(err)
}
//defer clearTransaction(tx)
insertText := "INSERT INTO services_status(id, cost, status,ip,area) VALUES (?,?,?,?,?)"
//rs, err := db.Exec(insertText, 4, "juejin", 1000)
//stmt, err := db.Prepare("INSERT INTO services_status(id, cost, status,ip,area) VALUES (?,?,?,?,?)")
//
//defer stmt.Close()
//if err != nil {
// log.Panic("db prepare faild!", err)
//}
for _, row := range s {
//rs, _ := stmt.Exec(row.Id, row.Cost, row.Status, row.Ip, row.Area)
rs, err := db.Exec(insertText, row.Id, row.Cost, row.Status, row.Ip, row.Area)
if err != nil {
log.Panic(err)
}
if id, _ := rs.LastInsertId(); id > 0 {
fmt.Println("插入成功")
}
//stmtTx := tx.Stmt(stmt)
//defer stmtTx.Close()
//var rs sql.Result
//for _, row := range s {
// rs, _ = stmtTx.Exec(row.Id, row.Cost, row.Status, row.Ip, row.Area)
// _ = tx.Commit()
// if n, _ := rs.RowsAffected(); n > 0 {
// fmt.Println("成功")
}
}

View File

@ -26,31 +26,50 @@ func init() {
}
type ServiceStatus struct {
id int32 `service id`
costtime int32 `monitor cost time second`
status bool `serivces status`
clientip string `monitor run client ip`
client_area string `client area`
func test_db_query() {
//var m mmysql.ServiceList
vv, err := mmysql.GetServiceList("bj")
if err != nil {
Error.Println(err)
log.Panic("conn mysql failed!")
}
jsons, errs := json.Marshal(vv)
if errs != nil {
Error.Println(errs.Error())
log.Panic("conver struct failed!", vv)
}
Info.Println(string(jsons))
}
func GetServiceStatus(c *ServiceStatus) bool {
Info.Println("get message from client", c)
return true
}
func SaveServiceStatus() bool {
return true
func test_db_insert() {
var v3 []mmysql.ServiceStatus
v1 := mmysql.ServiceStatus{
Id: 1,
Cost: 1199,
Ip: "192.168.100.1",
Status: true,
Area: "bj",
}
v2 := mmysql.ServiceStatus{
Id: 1,
Cost: 2000,
Ip: "192.168.100.2",
Status: true,
Area: "bj",
}
v3 = append(v3, v1)
v3 = append(v3, v2)
mmysql.SaveServiceStatus(v3)
}
func main() {
Info.Println("run main")
//var m mmysql.ServiceList
vv := mmysql.GetServiceList("bj")
jsons, errs := json.Marshal(vv)
if errs != nil {
Error.Println(errs.Error())
}
Info.Println(string(jsons))
//test_db_query()
test_db_insert()
//Warning.Println(string(jsons))
//for i := 0; i < len(vv); i++ {