操作系統 : CentOS7.3.1611_x64 go語言版本:1.8.3 linux/amd64 db2版本: db2_v101_linuxx64_expc 問題描述 怎麼使用go語言在CentOS環境下操作db2資料庫? 解決方案 go語言操作資料庫相關信息可以參考如下鏈接: https:// ...
操作系統 : CentOS7.3.1611_x64
go語言版本:1.8.3 linux/amd64
db2版本: db2_v101_linuxx64_expc
問題描述
怎麼使用go語言在CentOS環境下操作db2資料庫?
解決方案
go語言操作資料庫相關信息可以參考如下鏈接:
https://github.com/golang/go/wiki/SQLDrivers
使用db2cli
db2cli地址: https://bitbucket.org/phiggins/go-db2-cli/src
下載源碼,並將源碼放到如下路徑:
/usr/local/go/src/bitbucket.org/phiggins/go-db2-cli
[root@localhost go-db2-cli]# pwd /usr/local/go/src/bitbucket.org/phiggins/go-db2-cli [root@localhost go-db2-cli]# ls LICENSE mgodbc.go README.md [root@localhost go-db2-cli]#
這裡直接使用bitbucket提供的示例代碼 (db2test.go):
package main import ( _ "bitbucket.org/phiggins/go-db2-cli" "database/sql" "flag" "fmt" "os" "time" ) var ( connStr = flag.String("conn", "", "connection string to use") repeat = flag.Uint("repeat", 1, "number of times to repeat query") ) func usage() { fmt.Fprintf(os.Stderr, `usage: %s [options] %s connects to DB2 and executes a simple SQL statement a configurable number of times. Here is a sample connection string: DATABASE=MYDBNAME; HOSTNAME=localhost; PORT=60000; PROTOCOL=TCPIP; UID=username; PWD=password; `, os.Args[0], os.Args[0]) flag.PrintDefaults() os.Exit(1) } func execQuery(st *sql.Stmt) error { rows, err := st.Query() if err != nil { return err } defer rows.Close() for rows.Next() { var t time.Time err = rows.Scan(&t) if err != nil { return err } fmt.Printf("Time: %v\n", t) } return rows.Err() } func dbOperations() error { db, err := sql.Open("db2-cli", *connStr) if err != nil { return err } defer db.Close() st, err := db.Prepare("select current timestamp from sysibm.sysdummy1") if err != nil { return err } defer st.Close() for i := 0; i < int(*repeat); i++ { err = execQuery(st) if err != nil { return err } } return nil } func main() { flag.Usage = usage flag.Parse() if *connStr == "" { fmt.Fprintln(os.Stderr, "-conn is required") flag.Usage() } if err := dbOperations(); err != nil { fmt.Fprintln(os.Stderr, err) } }
[root@localhost db2Test]# ls build.sh db2Test.go run.sh [root@localhost db2Test]# ./build.sh [root@localhost db2Test]# ./run.sh Time: 2017-09-25 22:41:21.701142 +0000 UTC [root@localhost db2Test]#
build.sh :
#!/bin/bash DB2HOME=/home/db2inst1/sqllib export CGO_LDFLAGS=-L$DB2HOME/lib export CGO_CFLAGS=-I$DB2HOME/include go build db2Test.go
run.sh :
#! /bin/bash DB2HOME=/home/db2inst1/sqllib export LD_LIBRARY_PATH=$DB2HOME/lib ./db2Test -conn 'DATABASE=testdb; HOSTNAME=127.0.0.1; PORT=50000; PROTOCOL=TCPIP; UID=db2inst1; PWD=123456;'
好,就這些了,希望對你有幫助。
本文github地址:
https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017/20171202_使用go語言操作db2.rst
歡迎補充