Linux下GO通过ODBC连接到GBase 8s数据库

操作系统:CentOS 7.8 64-bit
软件需求:
GBase 8s CSDK 2.0.1A2_2
unixODBC 2.3.1

1,系统环境确认

1.1 unixODBC版本

Linux下root用户通过rpm确认unixODBC、unixODBC-devel均已经安装,如下:

[root@a01 ~]# rpm -qa unixODBC unixODBC-devel
unixODBC-2.3.1-14.el7.x86_64
unixODBC-devel-2.3.1-14.el7.x86_64

1.2 GBase 8s CSDK已经安装

Linux下gbasedbt用户通过esql -V命令确认GBase 8s CSDK已经安装,如下:

[gbasedbt@a01 ~]$ esql -V
GBase CSDK Version 4.10, GBASE-ESQL Version 4.10.FC4G1_2.0.1A2_2
Software Serial Number AAA#B000000

2,ODBC配置

2.1 ODBC配置文件

修改/etc/odbc.ini配置文件,增加GBase 8s数据库连接信息,特别注意需要的环境变量都加上。

;---------------------------------------------------------------------------
; GBase ODBC Sample File
;
; File:         odbc.ini
;
;---------------------------------------------------------------------------
[ODBC Data Sources]
utf8=GBase ODBC DRIVER
;
; Define ODBC Database Driver's Below - Driver Configuration Section
;
[utf8]
Driver=/opt/gbase/lib/cli/iclis09b.so
Description=GBase ODBC DRIVER
Database=utf8
LogonID=gbasedbt
pwd=GBase123
Servername=gbase01
CursorBehavior=0
CLIENT_LOCALE=zh_CN.utf8
DB_LOCALE=zh_CN.utf8
GL_USEGLU=1
TRANSLATIONDLL=/opt/gbase/lib/esql/igo4a304.so
;
; UNICODE connection Section
;
[ODBC]
;uncomment the below line for UNICODE connection
;UNICODE=UCS-4
;
; Trace file Section
;
Trace=0
TraceFile=/tmp/odbctrace.out
InstallDir=/opt/gbase
TRACEDLL=idmrs09a.so

2.2 验证ODBC配置

加载GBase 8s CSDK环境变量,包含连接到GBase 8s数据库所需的GBASEDBTDIR、LD_LIBRARY_PATH,以及选用的字符集DB_LOCALE、CLIENT_LOCALE、LANG等。加载ODBCINI环境变量。环境变量内容可保存为配置文件,如:gbase8s_odbc_env,以供调用。

export GBASEDBTDIR=/opt/gbase                    # GBase 8s CSDK安装目录
# 数据库使用的字符集
export DB_LOCALE=zh_CN.utf8
export CLIENT_LOCALE=zh_CN.utf8
export GL_USEGLU=1
 
export LD_LIBRARY_PATH=$GBASEDBTDIR/lib:$GBASEDBTDIR/lib/cli:$GBASEDBTDIR/lib/esql:$LD_LIBRARY_PATH
# ODBCINI
export ODBCINI=/etc/odbc.ini

通过isql命令检查ODBC配置

[root@a01 ~]#  . gbase8s_odbc_env
[root@a01 ~]#  isql -v utf8
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> select dbservername::varchar(40) from dual;
+-----------------------------------------+
|                                         |
+-----------------------------------------+
| gbase01                                 |
+-----------------------------------------+
SQLRowCount returns -1
1 rows fetched

3,安装GO和GO-ODBC

3.1 安装GO

GO下载地址:https://golang.google.cn/dl/
通过wget下载,并解压至/usr/local目录下

[root@a01~]# wget https://dl.google.com/go/go1.15.3.linux-amd64.tar.gz

[root@a01~]# tar -C /usr/local -xzf go1.15.3.linux-amd64.tar.gz

设置环境变量文件(也可以写入到/etc/profile中)

# go的目录是GOROOT,GOPATH是工作空间
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH
export GOPATH=/root/go-odbc

3.2 安装GO-ODBC

在$GOROOT/src目录下安装GO-ODBC
GO-ODBC的下载地址:git://github.com/weigj/go-odbc.git

[root@a01 ~] cd $GOROOT/src
[root@a01 src]# git clone git://github.com/weigj/go-odbc.git odbc

[root@a01 src]# cd odbc
[root@a01 odbc]# go install

[root@a01 odbc]# go list
odbc

4,编写程序测试

编写测试脚本程序TestGoOdbc.go

package main

import (
        "fmt"
        "database/sql"
        _ "odbc/driver"
)

func main() {
        fmt.Printf("GO ODBC测试程序开始运行.\n\n");
        db, err := sql.Open("odbc", "DSN=utf8;UID=gbasedbt;PWD=GBase123")
        if err != nil {
                panic(err)
        }
        defer db.Close()

        rows, err := db.Query("SELECT tabid,tabname FROM systables where tabid < 10")
        checkErr(err)

        for rows.Next() {
               var tabid int
               var tabname string
               err = rows.Scan(&tabid, &tabname)
               checkErr(err)
               fmt.Printf("%d\t%s\n", tabid, tabname)
        }
        rows.Close()

        fmt.Printf("\nGO ODBC测试程序结束运行.\n");
}

func checkErr(err error) {
        if err != nil {
               panic(err)
        }
}

执行测试程序

[root@a01 go-odbc]# go run TestGoOdbc.go 
GO ODBC测试程序开始运行.

1    systables
2    syscolumns
3    sysindices
4    systabauth
5    syscolauth
6    sysviews
7    sysusers
8    sysdepend
9    syssynonyms

GO ODBC测试程序结束运行.

标签: GBase 8s安装, go, go-odbc

添加新评论