mysql資料匯出golang實現

2021-09-24 17:46:24 字數 3850 閱讀 9631

這個mysql建表語句轉golang的struct工具挺好用的。

mysql資料匯出為excel檔案,golang實現:

****** install the package to your $gopath with the go tool from shell:

$ go get -u github.com/go-sql-driver/mysql
**具體說明請看:** [庫位址]( [wiki說明](

**示例如下,用到了go的flag包的能力,傳入命令列引數。具體看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

**:

package main

// 從mysql中匯出資料到csv檔案。

nil//values:一行的所有值,把每一行的各個字段放到values中,values長度==列數

values :=

make([

]sql.rawbytes,

len(columns)

)// print(len(values))

scanargs :=

make([

]inte***ce

,len

(values)

)for i :=

range values

//存所有行的內容totalvalues

totalvalues :=

make([

]string,0

)for rows.

next()

for_

, v :=

range values

totalvalues =

(totalvalues, s)

}if err = rows.

err(

); err !=

nilwritetocsv

(table+

".csv"

, columns, totalvalues)

ch true

}//writetocsv

func

writetocsv

(file string

, columns [

]string

, totalvalues [

]string

)"\xef\xbb\xbf")

w := csv.

newwriter

(f)for i, row :=

range totalvalues

else

} w.

flush()

fmt.

println

("處理完畢:"

, file)

}func

usage()

操作示例:

編譯**生成可執行檔案:

資料庫中有test2庫下的test表:

匯出其中的資料:

匯出結果如下:

這個mysql建表語句轉golang的struct工具挺好用的。

mysql 匯出資料

方法一 select.into outfile mysql select from mytbl into outfile tmp mytbl.txt 檢視mytbl.txt中內容如下 mysql system cat tmp mytbl.txt1 name1 2 name2 3 n 匯出的檔案中資料...

MySQL資料匯出

方法一 select.into outfile mysql select from mytbl into outfile tmp mytbl.txt query ok,3 rows affected 0.00 sec 檢視mytbl.txt中內容如下 mysql system cat tmp myt...

MySql資料匯出

mysql use caida 選擇要操作的資料庫 mysql select ip,node,physical cpu,cpu cores,processor,cpumodel,throughtput,use processor,use memory,use disk from three open...