Go   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Golang 如何操作DB2的?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

go-db2-example

IBM DB2是款商用的db,支持的编程语言中没有Go,也没有介绍过,今天就演示下Go通过odbc方式连接IBM DB2的例子。

DB2 ODBC driver

安装DB2 ODBC driver

DB2 ODBC driver的来源有一下几种:

  • db2安装包自带odbc驱动,和常用的jdbc驱动一样,odbc驱动一般都是在安装包中自带
  • 此外,一些单独的db2套件也含有odbc驱动,如:DB2 Application Development Client,Db2 Run-time Client,Db2 Data Server Runtime client等等
  • 单独的免安装odbc驱动包:db2 driver for ODBC Cli

下载macos64_odbc_cli.tar.gz,解压到任意目录下;

创建Data source配置

db2 有两种data source配置文件,具体路径在<install-odbc-dir>/cfg下面

  • db2cli.ini # text格式,该文件可用于所有的odbc驱动
  • db2dsdriver.cfg # xml格式, 9.5 版本后引入的,可以公DB2 Data Server Driver for ODBC and CLI使用

db2cli.ini范例

; db2cli.ini data source
[DB2_SAMPLE]
Database=SAMPLE
Protocol=TCPIP
Port=50000
Hostname=my-db2-machine
UID=my-OS-user
PWD=my-OS-password

db2dsdriver.cfg范例

<parameter name="name" value="value"/>
<!--  db2dsdriver.cfg data source -->
<configuration>
   <dsncollection>
      <dsn alias="DB2_SAMPLE" name="SAMPLE" host="my-db2-machine" port="50000">
         <parameter name="UserID" value="my-db2-user"/>
         <parameter name="password" value="my-db2-password"/>
      </dsn>
   </dsncollection>
</configuration>

配置完成后,验证下文件是否正确

$ cd /home/myuser/db2/odbc_cli/clidriver/bin/
$ ./db2cli validate -dsn DB2_SAMPLE
 db2cli validate -dsn sample

===============================================================================
Client information for the current copy:
===============================================================================

Client Package Type       : IBM Data Server Driver For ODBC and CLI
Client Version (level/bit): DB2 v10.5.0.5 (special_35187/64-bit)
Client Platform           : Darwin
Install/Instance Path     : .../clidriver
DB2DSDRIVER_CFG_PATH value: <not-set>
db2dsdriver.cfg Path      : .../clidriver/cfg/db2dsdriver.cfg
DB2CLIINIPATH value       : <not-set>
db2cli.ini Path           : .../clidriver/cfg/db2cli.ini
db2diag.log Path          : .../clidriver/db2dump/db2diag.log

===============================================================================
db2dsdriver.cfg scheR_741_11845@a validation for the entire file:
===============================================================================

Note: The validation utility Could not find the configuration file
db2dsdriver.cfg. The file is searched at
".../clidriver/cfg/db2dsdriver.cfg".

===============================================================================
db2cli.ini validation for data source name "sample":
===============================================================================

[ Keywords used for the connection ]

Keyword                   Value
---------------------------------------------------------------------------
DATABASE                  sample
PROTOCOL                  TCPIP
HOSTNAME                  127.0.0.1
serviCename               50000
UID                       db2inst1
PWD                       ********

Test下连接

$ echo "SELEct count(1) from syscat.tables" |db2cli execsql -dsn sample [ -user *** -passwd *** ]
IBM DATABASE 2 Interactive CLI Sample Program
(C) COPYRIGHT International Business Machines Corp. 1993,1996
All Rights Reserved
Licensed Materials - Property of IBM
US Government Users ReStricted Rights - Use,duplication or
disclosure reStricted by GSA ADP schedule Contract with IBM Corp.
> SELEct count(1) from syscat.tables
FetchAll:  columns: 1
  1
  643
FetchAll: 1 rows fetched.

unixODBC

首先要安装unixODBC驱动管理器,该odbc管理器是开源项目,能够帮助非win平台下的用户轻松使用odbc访问目标数据库.

与其它连接方式不同的是,ODBC应用程序app通常加载链接到ODBC驱动程序管理器而不是特定的ODBC驱动程序。
ODBC驱动程序管理器是ODBC应用程序和ODBC驱动程序之间的接口和桥梁。

在运行时,应用程序提供了一个连接字符串,即DSN,该连接字符串定义了要连接的ODBC数据源,并依次定义将处理连接的ODBC驱动程序。
unixODBC加载所请求的ODBC驱动程序并将所有ODBC API调用传递给目标驱动程序,也就是db2 odbc driver;
流程如下:
app ---> unixODBC ---> db2 ODBC driver

对于DB2 ODBC驱动程序来说,ODBC应用程序需要提供一个与DB2 ODBC驱动程序数据源同名的ODBC数据源。
unixODBC加载相应的数据源驱动程序(DB2 ODBC driver),并将数据源配置信息传递给加载的DB2 ODBC driver,DB2 ODBC驱动程序检查它的数据源的配置文件,判断它的名称与它传递的名称相同;

安装unixODBC

brew install unixODBC

注册DB2 ODBC driver和数据源

查看odbc配置文件

$ odbcinst -j
unixODBC 2.3.6
DRIVERs............: /usr/local/etc/odbcinst.ini
SYstem DATA sourcES: /usr/local/etc/odbc.ini
FILE DATA sourcEs..: /usr/local/etc/ODBCDatasources
USER DATA sourcEs..: .../.odbc.ini
sqlULEN Size.......: 8
sqlLEN Size........: 8
sqlSETPOSIROW Size.: 8

其中,odbcinst.ini配置范例如下:

# Example odbcinst.ini driver deFinition for DB2 Data Server Driver for ODBC and
# CLI
[DB2]
Description=DB2 ODBC Driver
Driver=/usr/lib/libdb2.so # replace /usr/lib with the directory where your
                          # driver shared object is located.
Fileusage=1               #
Dontdlclose=1             # IBM recommend setTing Dontdlclose to 1,which stops
                          # unixODBC unloading the ODBC Driver on disconnect.
                          # Note that in unixODBC 2.2.11 and later,the Driver
                          # Manager default for Dontdlclose is 1.

~/.odbc.ini配置如下:

[DB2_SAMPLE]
Driver=DB2
- 首先,unixODBC会加载DB2 ODBC driver驱动,
- 之后,DB2 ODBC driver会在db2cli.ini/db2dsdriver.cfg找相同名字的dns的相关配置;
- 若用户程序提供的用户名和密码,配置文件中的用户名和密码会忽略。

如下isql是unixODBC自带的ODBC程序,通过它可以验证dsn的配置是否正确,连接是否ok;

$ isql -v DB2_SAMPLE username password
错误1:说明~/.odbc.ini配置文件中的Data source Name(DSN)没有找到;
[IM002][unixODBC][Driver Manager]Data source name not found,and no default driver specified
[Isql]ERROR: Could not sqlConnect

$ isql -v DB2_SAMPLE username password
错误2:说明DB2 ODBC driver配置文件没有找到匹配的DSN名字的配置信息
[     ][unixODBC][IBM][CLI Driver] sql1531N  The connection Failed because the name specified
with the DSN connection String keyword Could not be found in either the db2dsdriver.cfg configuration file or the db2cli.ini configuration file.
Data source name specified in the connection String: "DB2_SAMPLE".

$ isql -v DB2_SAMPLE
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| Help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
sql>

使用go连接db2

见源码

PS:对于win下的go,抽空再尝试。

大佬总结

以上是大佬教程为你收集整理的Golang 如何操作DB2的?全部内容,希望文章能够帮你解决Golang 如何操作DB2的?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。