dongpo2340 2018-11-26 09:58
浏览 413
已采纳

使用Windows使用Golang与db2服务器连接

I'm trying to make connection to a Db2 database using golang. I'm using package to make connection and using the Db2 client called v10.1fp6_ntx64_odbc_cli.

However when I see the instruction on phiggins package it suggest to rebuild package after providing few environment variables

#!/bin/bash

 DB2HOME=$HOME/sqllib
 export CGO_LDFLAGS=-L$DB2HOME/lib
 export CGO_CFLAGS=-I$DB2HOME/include

 go build .

I'm new to golang and not sure how to perform this on Windows. I tried doing it using setting up environment variable but does not work. getting error

panic: Failed to load db2cli64.dll: The specified module could not be found. goroutine 1 [running]: syscall.(*LazyProc).mustFind(0xc000060270) C:/Go/src/syscall/dll_windows.go:280 +0x5f syscall.(*LazyProc).Addr(0xc000060270, 0x64) C:/Go/src/syscall/dll_windows.go:287 +0x32 bitbucket.org/phiggins/db2cli/api.SQLAllocHandle(0x4f0001, 0x0, 0xc000071ef8, 0xc000071f00)
C:/Users/myuser/go/src/bitbucket.org/phiggins/db2cli/api/zapi_windows.go:44 +0x34 bitbucket.org/phiggins/db2cli.initDriver(0x0, 0x0) C:/Users/myuser/go/src/bitbucket.org/phiggins/db2cli/driver.go:26 +0x57 bitbucket.org/phiggins/db2cli.init.0() C:/Users/myuser/go/src/bitbucket.org/phiggins/db2cli/driver.go:52 +0x29 exit status 2

Please help me to solve this , Looking to connect to a Db2 database using golang.

  • 写回答

1条回答 默认 最新

  • duanfeigui6655 2018-11-26 11:18
    关注

    The following steps let me connect to Db2 from golang on Windows7 pro x64. Your mileage may vary.

    I Used a recent "Db2 Runtime Client" package for Windows x64 downloadable from IBM website (in my case that was version 11.1.3.3b) and the filename I downloaded was v11.1.3fp3b_ntx64_rtcl.exe

    This is installed locally at location "c:\Program Files\IBM\sqllib" location (default).

    I have already a working gcc installed on Windows7 for other projects (it may be from minGW).

    I verified in a db2cmd.exe window that I can successfully connect via the CLP (which uses CLI) to the database of interest, using a specific port number and specific credentials. This way I know that the Db2-client is installed properly, and the Db2-server is running, the database is available, and credentials and port are all OK.

    Running as local administrator, Installed go for Windows from msi file downloaded from golang.org/dl to default location c:\go.

    Downloaded phiggins-db2cli-*.zip from https://bitbucket.org/phiggins/db2cli

    Created local directory: mkdir %userprofile%\go\src\bitbucket.org\phiggins\db2cli

    Unzipped contents of the phiggins-db2cli-*.zip into %userprofile%\go\src\bitbucket.org\phiggins\db2cli

    In a db2cmd.exe window running as Adminstrator:

    cd %userprofile%\go\src\bitbucket.org\phiggins\db2cli
    

    Created a batch file buildgo.bat with the following contents:

    set DB2HOME="C:\program files\ibm\sqllib"
    
    set CGO_LDFLAGS=-L"C:\Program Files\IBM\sqllib\lib"
    
    set CGO_CFLAGS=-I"C:\Program Files\IBM\sqllib\include"
    
    go build .
    

    In a db2cmd window running as administrator, ran the batchfile, it completed without errors or messages with errorlevel 0.

    In the same db2cmd window, created a file containing the sample program shown at https://bitbucket.org/phiggins/db2cli

    The file I created was called testgo.go

    I then execute the sample program:

    go run testgo.co -conn DATABASE=sample;HOSTNAME=whatever;PORT=60000;UID=whatever;PWD=whatever;
    

    --> successfully get the result set on screen

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路