1. 安装驱动
go get -d github.com/asifjalil/cli
2. liteide 使用 win32 编译环境,修改 win32.env,添加 (根据本机DB 9.7 的安装路径改动)
DB2HOME=D:/PROGRA~2/IBM/sqlLIB
CGO_LDFLAGS=-L%DB2HOME%/lib
CGO_CFLAGS=-I%DB2HOME%/include
3. 打开$GOPATH/src/github.com/asifjalil/cli 目录 ,替换所有 *.go 文件中的
#cgo LDFLAGS: -ldb2
为
#cgo LDFLAGS: -ldb2cli
#cgo LDFLAGS: -ldb2api
4. cirl+F8 (go install)
5. 测试代码运行
package main
import (
"database/sql"
"fmt"
_ "github.com/asifjalil/cli"
)
func main() {
connStr := `Driver={IBM DB2 ODBC Driver};Hostname=localhost;Port=50000;Protocol=TCPIP;Database=OATEST;CurrentSchema=oa;UID=oa;PWD=oa;`
db,err := sql.Open("cli",connStr)
if err != nil {
panic(err)
}
defer db.Close()
sql := "select STAFFID,STAFFNAME from WF_STAFF where STAFFID='ADMIN'"
fmt.Println(sql)
type rslt struct {
STAFFID string
STAFFNAME string
}
rows,err := db.Query(sql)
if err != nil {
panic(err)
}
for rows.Next() {
var r rslt
err = rows.Scan(&r.STAFFID,&r.STAFFNAME)
if err != nil {
panic(err)
}
fmt.Println(r)
}
}