脚本如下,简单易用:
Go
package main
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type BaseID struct {
ID int64 `gorm:"primaryKey" json:"id"`
}
type Tasks struct {
BaseID
UUID string `gorm:"column:uuid;not null;unique;index" json:"uuid"`
Name string `gorm:"column:name;type:varchar(128)" json:"name"`
CreateTime float64 `gorm:"column:create_time" json:"create_time"`
}
func main() {
// 连接 MySQL 数据库
dsn := "root:**********@tcp(192.68.1.1:3306)/skyeye?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic(err.Error())
}
// 查询数据行数
var count int64
result := db.Model(&Tasks{}).Count(&count)
//db.DBO.Model(&oldModels.Tasks{}).Where("is_delete = 0").Count(&totalCount)
if result.Error != nil {
panic(result.Error.Error())
}
fmt.Printf("表中共有 %d 行数据\n", count)
}
将Golang脚本编译成可执行二进制文件的命令:
windows:
bash
# 要求在cmd命令行下执行 - 否则会报错
SET CGO_ENABLED=0&&SET GOOS=linux&&SET GOARCH=amd64&& go build -o query_count ./main.go
SET CGO_ENABLED=0&&SET GOOS=windows&&SET GOARCH=amd64&& go build -o query_count.exe ./main.go
linux:
bash
env GOOS=linux GOARCH=amd64 GO111MODULE=on GOPROXY=https://goproxy.cn,direct go build -ldflags "-s -w" -o query_count main.go