Golang + windows 7 access DB2 9.7

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1. Install the driver

go get -d github.com/asifjalil/cli

2. Liteide using Win32 compilation environment, modify WIN32.ENV, add (change according to the installation path of native DB 9.7)

Db2home=d:/progra~2/ibm/sqllib
Cgo_ldflags=-l%db2home%/lib
Cgo_cflags=-i%db2home%/include

3. Open the $GOPATH/src/github.com/asifjalil/cli directory, replacing all of the *.go files

#cgo Ldflags:-LDB2

For

#cgo Ldflags:-ldb2cli
#cgo Ldflags:-ldb2api

4. Cirl+f8 (Go Install)

5. Test code Run

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)
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.