package main /* 思路: 脚本按照顺序拼接,旧文件按照 utf8 格式清空 */ import ( "database/sql" log2 "log" "os" "time" _ "github.com/go-sql-driver/mysql" ) //存在所有对象信息 - 集合 func main() { db, _ := sql.Open("mysql", "hy_qggwy:hy_qggwy@tcp(192.168.0.110:35017)/hy_qggwy?charset=utf8") log2.Print("操作数据库连接:", db) // db.Exec("source E:/hy_qggwy_kk.sql") QuerySQL(db, "select * from a01 where a0184 in (select d from kk)", "insert into a01 values", "a01") QuerySQL(db, "select * from a02 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "insert into a02 values", "a02") QuerySQL(db, "select * from a05 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "insert into a05 values", "a05") QuerySQL(db, "select * from a06 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "insert into a06 values", "a06") QuerySQL(db, "select * from a08 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "insert into a08 values", "a08") QuerySQL(db, "select * from a14 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "insert into a14 values", "a14") QuerySQL(db, "select * from a15 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "insert into a15 values", "a15") QuerySQL(db, "select * from a36 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "insert into a36 values", "a36") QuerySQL(db, "select * from a99z1 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "insert into a99z1 values", "a99z1") QuerySQL(db, "select * from a30 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "insert into a30 values", "a30") QuerySQL(db, "select * from a33 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "insert into a33 values", "a33") } // 获取表数据 func QuerySQL(db *sql.DB, sqlStr, insertSQL, tableName string) { rows, err := db.Query(sqlStr) log2.Print("开始备份数据:", tableName, "处理数据:", rows, "异常情况:", err) defer rows.Close() cloumns, _ := rows.Columns() values := make([]sql.RawBytes, len(cloumns)) scanArgs := make([]interface{}, len(values)) for i := range values { scanArgs[i] = &values[i] } aa := "" for rows.Next() { _ = rows.Scan(scanArgs...) value, v := "(", "" aa = aa + insertSQL for _, col := range values { if col == nil { v = "" } else { v = string(col) } if value == "(" { value = value + "'" + v } else { value = value + "','" + v } } value = value + "');\n" aa = aa + value } var path string path = "./" + tableName + "_" + time.Now().Format("2006102150405") + ".sql" file3, _ := os.Create(path) file3.WriteString(aa) file3.Close() }