MySQL data export to Excel file, Golang implementation:
First download the dependent three-party library:
Simple Install the "your $GOPATH with the" Go tool from Shell:
$ go get -u github.com/go-sql-driver/mysql
For specific instructions, see:
Library Address
Wiki description
code example below, the ability to use the flag packet of Go, passed the command line parameters. specifically see Helpinfo:
Usage of mysqldataexport: -port int the port for mysql,default:32085 -addr string the address for mysql,default:10.146.145.67 -user string the username for login mysql,default:dbuser -pwd string the password for login mysql by the username,default:Admin@123 -db string the port for me to listen on,default:auditlogdb -tables string the tables will export data, multi tables separator by comma, default:op_log,sc_log,sys_log
Code:
Package main//export data from MySQL to a CSV file. Import ("Database/sql" "Encoding/csv" "FMT" "Os" _ "Github.com/go-sql-driver/mysql" "Flag" "strings" var (tables = make ([]string, 0) datasourcename = "") const (drivernamemysql = "MySQL" helpinfo = ' Us Age of Mysqldataexport:-port int, the port for Mysql,default:32085-addr string, the address for Mysql,defa Ult:10.146.145.67-user string The username for login Mysql,default:dbuser-pwd string The password for L Ogin MySQL by the username,default:admin@123-db string the port for me to listen on,default:auditlogdb-tables String the tables would export data, multi tables separator by comma, Default:op_log,sc_log,sys_log ') func init ( {Port: = flag. Int ("Port", 32085, "The Port for mysql,default:32085") Addr: = flag. String ("addr", "10.146.145.67", "The Address for mysql,default:10.146.145.67") User: = Flag. String ("User", "Dbuser", "the username for loginMysql,default:dbuser ") pwd: = flag. String ("pwd", "admin@123", "The password for login MySQL by the username,default:admin@123") DB: = flag. String ("db", "Auditlogdb", "The port for me to listen On,default:auditlogdb") Tabs: = flag. String ("Tables", "Op_log,sc_log,sys_log", "the tables would export data, multi tables separator by comma, DEFAULT:OP_LOG,SC _log,sys_log ") flag. Usage = Usage flag. Parse () tables = append (tables, strings. Split (*tabs, ",") ...) DataSourceName = FMT. Sprintf ("%s:%s@tcp (%s:%d)/%s?charset=utf8", *user, *pwd, *addr, *port, *db)}func main () {count: = len (tables) ch: = Make (chan bool, count) db, err: = SQL. Open (Drivernamemysql, datasourcename) defer db. Close () if err! = Nil {panic (err). Error ())}//Open doesn ' t open a connection. Validate DSN data:err = db. Ping () if err! = Nil {panic (err). Error ())} for _, table: = Range Tables {Go Querysql (db, table, CH)} for I: = 0; I < count; i++ { <-ch} fmt. Println ("done!")} Func querysql (db *sql. DB, table string, CH chan bool) {FMT. Println ("Start processing:", table) rows, err: = db. Query (FMT. Sprintf ("Select * from%s", table)) if err! = Nil {panic (err)} columns, err: = rows. Columns () if err! = Nil {panic (err. Error ())}//values: All values of a row, place each field in the values, values length = = number of columns values: = Make ([]sql. Rawbytes, Len (columns))//print (Len (values)) Scanargs: = Make ([]interface{}, Len (values)) for I: = range values {Scanargs[i] = &values[i]}//Save the contents of all rows totalvalues totalvalues: = Make ([][]string, 0) for rows. Next () {///Save the contents of each row var s []string//Adds the contents of each row to the Scanargs, also added to the values err = rows. Scan (Scanargs ...) If err! = Nil {panic (err. Error ())} for _, V: = Range values {s = Append (s, string (v))//print (Len (s)) } totalvalues = Append (Totalvalues, s)} If Err = rows. ERR (); Err! = Nil {panic (err. Error ())} writetocsv (table+ ". csv", columns, totalvalues) ch <-true}//writetocsvfunc writetocsv (file string, columns []string, totalvalues [][]string] {f, err: = OS. Create (file)//FMT. PRINTLN (columns) defer f.close () if err! = Nil {panic (err)}//f.writestring ("\XEF\XBB\XBF") W: = C Sv. Newwriter (f) For I, row: = Range Totalvalues {//First write column name + first row data if I = = 0 {w.write (columns) W.write (Row)} else {w.write (row)}} w.flush () fmt. PRINTLN ("Processed:", file)}func usage () {FMT. Fprint (OS. Stderr, Helpinfo) flag. Printdefaults ()}