• Golang + Oracle 11g + XORM + go-oci8 的数据库驱动测试(超详细)
  • Golang + Oracle 11g + GORM(改) + go-oci8 的数据库驱动测试(超详细)

一、系统环境介绍:

  • Windows 10
  • Oracle 11g 64bit
  • OCI -> instantclient-basic-windows.x64-12.2.0.1.0
  • OCI SDK -> instantclient-sdk-windows.x64-12.1.0.2.0
  • MinGW 64bit
  • go1.11 windows/amd64

注意:我在Oracle 11g版本下使用 OCI 及 SDK 11.2.0.4.0版本会报错,改用 OCI 12.2.0.1.0 及 SDK 12.1.0.2.0就可以了。

二、安装MinGW,注意安装系统相应的32/64位的版本

1、下载地址:

https://sourceforge.net/projects/mingw-w64/files/

2、安装MinGW
  • 双击安装程序

    双击安装程序
  • 修改Architecture为:x86_64,点击 Next > 直到安装完成。注意:安装路径,后面需要用到
点击 Next >
  • 安装完成目录结构

    安装完成
  • 设置环境变量Path,添加:D:ToolMinGW64mingw64bin
设置环境变量
  • 下载 pkg-config.exe,并复制到D:ToolMinGW64mingw64bin目录下
    下载地址:https://raw.githubusercontent.com/wendal/go-oci8/master/windows/pkg-config.exe
    项目开源地址:https://github.com/wendal/go-oci8

    pkg-config.exe

    复制后如图:

复制位置

三、下载Oracle 的 OCI和SDK

1、下载OCI和SDK

https://www.oracle.com/technetwork/topics/winx64soft-089540.html

下载OCI和SDK文件
2、创建存放目录,如:C:instantclient_11_2,并将上面的两个压缩文件解压到C:instantclient_11_2中,如图:
解压的OCI和SDK
  • 设置环境变量Path,添加:C:instantclient_11_2

    设置环境变量
  • D:ToolMinGW64mingw64libpkg-config目录下新建:oci8.pc文件,pkg-config目录也需要新建。注意修改你的OCI目录地址
    新建的目录结构

    目录结构

    修改oci8.pc中的OCI路径C:instantclient_11_2

# Package Information for pkg-config
prefix=C:/instantclient_11_2
exec_prefix=C:/instantclient_11_2
libdir={exec_prefix}
includedir={prefix}/sdk/include/

Name: OCI
Description: Oracle database engine
Version: 11.2
Libs: -L{libdir} -loci
Libs.private: 
Cflags: -I{includedir}

设置环境变量PKG_CONFIG_PATH

image.png

四、数据操作测试

测试文件main_test.go,测试前注意先添加userinfo表及数据

package main_test

import (
    "database/sql"
    "testing"

    "github.com/go-xorm/xorm"
    _ "github.com/mattn/go-oci8"
)

var driverName = "oci8" //Oracle 驱动
var dataSourceName = "ggs/123456@127.0.0.1:1521/ORCL"  //数据库账号:ggs,密码:123456,实例服务:ORCL
var engine *xorm.Engine

func TestXormOracle(t *testing.T) {
    var err error
    engine, err = xorm.NewEngine(driverName, dataSourceName)
    if err != nil {
        t.Error(err)
    }
    tabs, err := engine.DBMetas()
    if err != nil {
        t.Error(err)
    }
    println(len(tabs)) 
}

func TestMattnOracle(t *testing.T) {
    var db *sql.DB
    var err error
    if db, err = sql.Open(driverName, dataSourceName); err != nil {
        t.Error(err)
        return
    }
    var rows *sql.Rows
    if rows, err = db.Query("select * from userinfo"); err != nil {
        t.Error(err)
        return
    }
    defer rows.Close()
    for rows.Next() {
        var id int
        var name string
        rows.Scan(&id, &name)
        println(id, name) // 3.14 foo
    }
}

1、TestXormOracle测试
=== RUN   TestXormOracle
oracle
[{"Name":"DEMO","Type":null,"Indexes":{"SYS_C0013542":{"IsRegular":false,"Name":"SYS_C0013542","Type":2,"Cols":["ID"]}},"PrimaryKeys":[],"AutoIncrement":"","Created":{},"Updated":"","Deleted":"","Version":"","Cacher":null,"StoreEngine":"","Charset":"","Comment":""},{"Name":"USERINFO","Type":null,"Indexes":{"SYS_C0022579":{"IsRegular":false,"Name":"SYS_C0022579","Type":2,"Cols":["id"]}},"PrimaryKeys":[],"AutoIncrement":"","Created":{},"Updated":"","Deleted":"","Version":"","Cacher":null,"StoreEngine":"","Charset":"","Comment":""}]
--- PASS: TestXormOracle (0.14s)
PASS

Process finished with exit code 0
2、TestMattnOracle测试
API server listening at: 127.0.0.1:63941
=== RUN   TestMattnOracle
1 张三
2 李四
--- PASS: TestMattnOracle (0.06s)
PASS

Debugger finished with exit code 2

五、错误问题及解决方案

1、OCI版本不一致,报错的相关内容:
In file included from D:/worktools/instantclient_11_2/sdk/include/oci.h:541, from ......mattngo-oci8oci8.go:4: 
D:/worktools/instantclient_11_2/sdk/include/oratypes.h:236:25: error: expected ' =', ',', ';', 'asm' or 'attribute' before 'ubig_ora' typedef unsigned _int64 ubig_ora; ^~~~ 
D:/worktools/instantclient_11_2/sdk/include/oratypes.h:237:25: error: expected ' =', ',', ';', 'asm' or 'attribute' before 'sbig_ora' typedef signed _int64 sbig_ora; ^~~~ In file included from 
D:/worktools/instantclient_11_2/sdk/include/oci.h:3045, from ......mattngo-oci8oci8.go:4: D:/worktools/instantclient_11_2/sdk/include/ociap.h:7459:40: error: unknown type name 'ubig_ora' OraText path, ubig_ora lenp ); ^~~~ 
D:/worktools/instantclient_11_2/sdk/include/ociap.h:7471:36: error: unknown type name 'ubig_ora' uword origin, ubig_ora offset, sb1 dir ); ^~~~ 
D:/worktools/instantclient_11_2/sdk/include/ociap.h:8278:23: error: unknown type name 'sbig_ora' sbig_ora bufferLength, sbig_ora returnLength, ^~~~ 
D:/worktools/instantclient_11_2/sdk/include/ociap.h:8278:46: error: unknown type name 'sbig_ora' sbig_ora bufferLength, sbig_ora returnLength

错误原因:OCI版本问题,我是在Oracle 11g版本下使用 OCI 及 SDK 11.2.0.4.0版本会报错
解决方法:改用 OCI 12.2.0.1.0 及 SDK 12.1.0.2.0就可以了。

2、fatal error: oci.h: No such file or directory
# github.com/mattn/go-oci8
/slview/nms/go/src/github.com/mattn/go-oci8/oci8.go:4:17: fatal error: oci.h: No such file or directory
 #include 
                 ^
compilation terminated.

错误原因:D:ToolMinGW64mingw64libpkg-configoci8.pc 配置文件中的路径未修改
解决方法:修改oci8.pc中的路径C:/instantclient_11_2为你的OCI存放路径,如下:

# Package Information for pkg-config
prefix=C:/instantclient_11_2
exec_prefix=C:/instantclient_11_2
libdir={exec_prefix}
includedir={prefix}/sdk/include/

Name: OCI
Description: Oracle database engine
Version: 11.2
Libs: -L{libdir} -loci
Libs.private: 
Cflags: -I{includedir}

文章来源于互联网,如有雷同请联系站长删除:Golang + Oracle 11g + XORM + go-oci8 的数据库驱动测试(超详细)

发表评论