Browse Source

实现基本的操作,实现导出数据文件

master
VIVIMAN 4 years ago
parent
commit
1c4d5d2dcd
  1. 93
      golang_learn/data_office/get_hy_sql/main.go

93
golang_learn/data_office/get_hy_sql/main.go

@ -6,7 +6,7 @@ package main
*/ */
import ( import (
"database/sql" "database/sql"
log2 "log" "log"
"os" "os"
"time" "time"
@ -15,27 +15,27 @@ import (
//存在所有对象信息 - 集合 //存在所有对象信息 - 集合
func main() { func main() {
db, _ := sql.Open("mysql", "hy_qggwy:hy_qggwy@tcp(192.168.0.110:35017)/hy_qggwy?charset=utf8") db, _ := sql.Open("mysql", "root:admin@tcp(127.0.0.1:35017)/hy_qggwy?charset=utf8")
log2.Print("操作数据库连接:", db) log.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 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))", "insert into a02 values", "a02") 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))", "insert into a05 values", "a05") 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))", "insert into a06 values", "a06") 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))", "insert into a08 values", "a08") 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))", "insert into a14 values", "a14") 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))", "insert into a15 values", "a15") 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))", "insert into a36 values", "a36") 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))", "insert into a99z1 values", "a99z1") 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))", "insert into a30 values", "a30") 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))", "insert into a33 values", "a33") 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, insertSQL, tableName string) { func QuerySQL(db *sql.DB, sqlStr, tableName string, colNil []string) {
rows, err := db.Query(sqlStr) rows, err := db.Query(sqlStr)
log2.Print("开始备份数据:", tableName, "处理数据:", rows, "异常情况:", err) log.Print("开始备份数据:", tableName, "处理数据:", rows, "异常情况:", err)
defer rows.Close() defer rows.Close()
cloumns, _ := rows.Columns() cloumns, _ := rows.Columns()
values := make([]sql.RawBytes, len(cloumns)) values := make([]sql.RawBytes, len(cloumns))
@ -46,22 +46,48 @@ func QuerySQL(db *sql.DB, sqlStr, insertSQL, tableName string) {
aa := "" aa := ""
for rows.Next() { for rows.Next() {
_ = rows.Scan(scanArgs...) _ = rows.Scan(scanArgs...)
value, v := "(", "" key, value := "(", "("
aa = aa + insertSQL aa = aa + "insert into " + tableName + " "
for _, col := range values { for i, col := range values {
if col == nil { kk := cloumns[i]
v = "" if inStr(colNil, kk) {
} else { continue
v = string(col)
} }
if value == "(" { if i == len(values)-1 {
value = value + "'" + v 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 { } else {
value = value + "','" + v 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) + "'"
}
}
} }
} }
value = value + "');\n" key = key + ")"
aa = aa + value value = value + ");\n"
aa = aa + key + " values " + value
} }
var path string var path string
path = "./" + tableName + "_" + time.Now().Format("2006102150405") + ".sql" path = "./" + tableName + "_" + time.Now().Format("2006102150405") + ".sql"
@ -69,3 +95,12 @@ func QuerySQL(db *sql.DB, sqlStr, insertSQL, tableName string) {
file3.WriteString(aa) file3.WriteString(aa)
file3.Close() file3.Close()
} }
func inStr(colNil []string, kk string) bool {
for _, s := range colNil {
if s == kk {
return true
}
}
return false
}

Loading…
Cancel
Save