Golang - 连接MySQL执行查询脚本

脚本如下,简单易用:

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:

# 要求在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:

env GOOS=linux  GOARCH=amd64 GO111MODULE=on GOPROXY=https://goproxy.cn,direct go build  -ldflags "-s -w" -o query_count  main.go

相关推荐

  1. Golang - 连接MySQL执行查询脚本

    2024-06-15 06:20:03       35 阅读
  2. Mysql连接查询

    2024-06-15 06:20:03       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-15 06:20:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-15 06:20:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-15 06:20:03       82 阅读
  4. Python语言-面向对象

    2024-06-15 06:20:03       91 阅读

热门阅读

  1. 【WSL开发】在WSL中部署jupyter lab并远程访问

    2024-06-15 06:20:03       30 阅读
  2. C语言FFT算法

    2024-06-15 06:20:03       35 阅读
  3. 嵌入式微处理器重点学习(二)

    2024-06-15 06:20:03       25 阅读
  4. React编写可复用图标组件

    2024-06-15 06:20:03       29 阅读
  5. LogicFlow 学习笔记——6. LogicFlow 基础 网格 Grid

    2024-06-15 06:20:03       26 阅读
  6. Android Display管理服务DMS

    2024-06-15 06:20:03       30 阅读
  7. CSS 媒体类型

    2024-06-15 06:20:03       25 阅读
  8. Spring框架的原理及应用详解(一)

    2024-06-15 06:20:03       27 阅读
  9. 数据库的隔离级别

    2024-06-15 06:20:03       28 阅读
  10. leetcode 70.爬楼梯

    2024-06-15 06:20:03       34 阅读
  11. 每周题解:Intervals(区间)

    2024-06-15 06:20:03       50 阅读
  12. dolphinscheduler独立集群部署文档(海豚调度)

    2024-06-15 06:20:03       29 阅读