一、系统环境介绍:
- 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、下载地址:
2、安装MinGW
x86_64Next >注意:安装路径,后面需要用到
点击 Next >
D:\Tool\MinGW64\mingw64\bin
设置环境变量
复制位置
三、下载Oracle 的 OCI和SDK
1、下载OCI和SDK
下载OCI和SDK文件
C:\instantclient_11_2C:\instantclient_11_2
解压的OCI和SDK
C:\instantclient_11_2D:\Tool\MinGW64\mingw64\lib\pkg-configoci8.pcpkg-configoci8.pcC:\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.gouserinfo
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 ......\mattn\go-oci8\oci8.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 ......\mattn\go-oci8\oci8.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版本会报错12.2.0.1.012.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 <oci.h>
^
compilation terminated.
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}