package main
import (
"database/sql"
"errors"
"fmt"
"github.com/go-ini/ini"
_ "github.com/go-sql-driver/mysql"
termbox "github.com/nsf/termbox-go"
"time"
)
//全局变量
var (
db *sql.DB
ip string
port string
username string
password string
database string
wait_time int
content string
)
//初始化ini文件
func initIni() error {
cfg, err := ini.Load("my.ini")
if err != nil {
return errors.New("ini文件读取失败")
}
ip = cfg.Section("mysql").Key("ip").String()
port = cfg.Section("mysql").Key("port").String()
username = cfg.Section("mysql").Key("username").String()
password = cfg.Section("mysql").Key("password").String()
database = cfg.Section("mysql").Key("database").String()
content = cfg.Section("mysql").Key("content").String()
wait_time, err = cfg.Section("mysql").Key("wait_time").Int()
if err != nil {
return errors.New("wait_time参数不是整数")
}
return nil
}
//初始化mysql
func initDB() (err error) {
var dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", username, password, ip, port, database)
//Open只会验证dsb的格式是否正确,不会验证是否连接成功,同理,密码是否正确也不知道
db, err = sql.Open("mysql", dsn)
if err != nil {
return errors.New("格式错误")
}
// 此时尝试连接数据库,会判断用户,密码,ip地址,端口是否正确
err = db.Ping()
if err != nil {
return errors.New("配置错误")
}
return nil
}
type Table struct {
//表名
TableName string
}
// 根据数据库获取当前数据库下所有表
func DatabaseToTables() ([]Table, error) {
var sqlStr = "show TABLES;"
rows, err := db.Query(sqlStr)
if err != nil {
return nil, errors.New("查询失败")
}
defer rows.Close()
var tables = make([]Table, 0, 100)
for rows.Next() {
var table Table
err = rows.Scan(&table.TableName)
if err != nil {
return nil, errors.New("绑定数据失败")
}
tables = append(tables, table)
}
return tables, nil
}
type Field struct {
//列名
ColumnName string
}
// 根据表名获取当前表所有字段
func TableToFields(table string) ([]Field, error) {
var sqlStr = "select COLUMN_NAME from information_schema.COLUMNS where table_name = '%s';"
sqlStr = fmt.Sprintf(sqlStr, table)
rows, err := db.Query(sqlStr)
if err != nil {
return nil, errors.New("查询失败")
}
defer rows.Close()
var fields = make([]Field, 0, 100)
for rows.Next() {
var field Field
err = rows.Scan(&field.ColumnName)
if err != nil {
return nil, errors.New("绑定数据失败")
}
fields = append(fields, field)
}
return fields, nil
}
//根据字段一一查询
func FieldSelect(table, field, content string) (map[string]string, error) {
//fmt.Println(table, field, content)
var sqlStr = "SELECT * from %s where %s like '%%%s%%';"
sqlStr = fmt.Sprintf(sqlStr, table, field, content)
rows, err := db.Query(sqlStr)
if err != nil {
return nil, errors.New("sql查询失败")
}
//函数结束释放链接
defer rows.Close()
//sql转map
result, err := ScanRow(rows)
if err != nil {
return nil, err
}
return result, nil
}
//获取单个
func ScanRow(rows *sql.Rows) (map[string]string, error) {
//读出查询出的列字段名
cols, _ := rows.Columns()
//values是每个列的值,这里获取到byte里
values := make([][]byte, len(cols))
//query.Scan的参数,因为每次查询出来的列是不定长的,用len(cols)定住当次查询的长度
scans := make([]interface{}, len(cols))
//让每一行数据都填充到[][]byte里面,狸猫换太子
for i := range values {
scans[i] = &values[i]
}
//获取单行必须Next()
rows.Next()
//传指针进去,但是不通过指针能获取值
err := rows.Scan(scans...)
if err != nil {
return nil, errors.New("Scan失败")
}
row := make(map[string]string)
for k, v := range values {
//cols[k]为字段名
key := cols[k]
row[key] = string(v)
}
return row, nil
}
func pause() {
fmt.Println("请按任意键继续...")
Loop:
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
break Loop
}
}
}
func main() {
//初始化ini
err := initIni()
if err != nil {
fmt.Println("错误:", err)
pause()
return
}
//初始化mysql
err = initDB()
if err != nil {
fmt.Println("错误:", err)
pause()
return
}
//开始时间
begin_time := time.Now()
// begin:主要逻辑,三层for循环
//根据数据库获取当前数据库下所有表
tables, err := DatabaseToTables()
if err != nil {
fmt.Println("错误:", err)
pause()
return
}
for _, table := range tables {
fields, err := TableToFields(table.TableName)
if err != nil {
fmt.Println("错误:", err)
pause()
return
}
for _, field := range fields {
_, err := FieldSelect(table.TableName, field.ColumnName, content)
//err != nil表示查询失败,继续下次查询
if err != nil {
continue
}
//fmt.Println(row)
fmt.Printf("表名:%s,列名:%s\n", table.TableName, field.ColumnName)
//睡眠指定时间
time.Sleep(time.Millisecond * time.Duration(wait_time))
}
}
elapsed_time := time.Now().Sub(begin_time)
fmt.Println("运行时间:", elapsed_time)
//任意键结束
pause()
//end:主要逻辑
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史