|
|
|
package main
|
|
|
|
|
|
|
|
/*
|
|
|
|
思路: 脚本按照顺序拼接,旧文件按照 utf8 格式清空
|
|
|
|
|
|
|
|
*/
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
)
|
|
|
|
|
|
|
|
//存在所有对象信息 - 集合
|
|
|
|
func main() {
|
|
|
|
db, _ := sql.Open("mysql", "root:admin@tcp(127.0.0.1:35017)/hy_qggwy?charset=utf8")
|
|
|
|
log.Print("操作数据库连接:", db)
|
|
|
|
|
|
|
|
QuerySQL(db, "select * from a01 where a0184 in (select d from kk)", "a01", []string{"A0155", "JSNLSJ", "TBSJ", "XGSJ"})
|
|
|
|
QuerySQL(db, "select * from a02 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a02", []string{})
|
|
|
|
QuerySQL(db, "select * from a05 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a05", []string{})
|
|
|
|
QuerySQL(db, "select * from a06 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a06", []string{})
|
|
|
|
QuerySQL(db, "select * from a08 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a08", []string{})
|
|
|
|
QuerySQL(db, "select * from a14 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a14", []string{})
|
|
|
|
QuerySQL(db, "select * from a15 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a15", []string{})
|
|
|
|
QuerySQL(db, "select * from a36 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a36", []string{})
|
|
|
|
QuerySQL(db, "select * from a99z1 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a99z1", []string{})
|
|
|
|
QuerySQL(db, "select * from a30 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a30", []string{})
|
|
|
|
QuerySQL(db, "select * from a33 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a33", []string{})
|
|
|
|
QuerySQL(db, "select * from a57 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a57", []string{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取表数据
|
|
|
|
func QuerySQL(db *sql.DB, sqlStr, tableName string, colNil []string) {
|
|
|
|
rows, err := db.Query(sqlStr)
|
|
|
|
log.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...)
|
|
|
|
key, value := "(", "("
|
|
|
|
aa = aa + "insert into " + tableName + " "
|
|
|
|
for i, col := range values {
|
|
|
|
kk := cloumns[i]
|
|
|
|
if inStr(colNil, kk) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i == len(values)-1 {
|
|
|
|
key = key + kk
|
|
|
|
if value == "(" {
|
|
|
|
if col == nil {
|
|
|
|
value = value + "NULL"
|
|
|
|
} else {
|
|
|
|
value = value + "'" + string(col) + "'"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if col == nil {
|
|
|
|
value = value + ",NULL"
|
|
|
|
} else {
|
|
|
|
value = value + ",'" + string(col) + "'"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
key = key + kk + ","
|
|
|
|
if value == "(" {
|
|
|
|
if col == nil {
|
|
|
|
value = value + "NULL"
|
|
|
|
} else {
|
|
|
|
value = value + "'" + string(col) + "'"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if col == nil {
|
|
|
|
value = value + ",NULL"
|
|
|
|
} else {
|
|
|
|
value = value + ",'" + string(col) + "'"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
key = key + ")"
|
|
|
|
value = value + ");\n"
|
|
|
|
aa = aa + key + " values " + value
|
|
|
|
}
|
|
|
|
var path string
|
|
|
|
path = "./" + tableName + "_" + time.Now().Format("2006102150405") + ".sql"
|
|
|
|
file3, _ := os.Create(path)
|
|
|
|
file3.WriteString(aa)
|
|
|
|
file3.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func inStr(colNil []string, kk string) bool {
|
|
|
|
for _, s := range colNil {
|
|
|
|
if s == kk {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|