一、系统环境介绍:
- 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:\Tool\MinGW64\mingw64\bin
设置环境变量
-
下载
pkg-config.exe,并复制到D:\Tool\MinGW64\mingw64\bin目录下下载地址: 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:\Tool\MinGW64\mingw64\lib\pkg-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}
四、数据操作测试
测试文件 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 ......\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版本会报错
解决方法:改用 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 <oci.h>
^
compilation terminated.
错误原因:D:\Tool\MinGW64\mingw64\lib\pkg-config\oci8.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}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 一文了解各种数据库默认驱动、URL、端口
- 超详细的各种数据库默认驱动、URL、端口总结
- YurunPHP 2.1 数据库驱动改为 PDO,放弃 PHP 5.3
- 时序数据库 TDengined 的 .Net Core 驱动 1.0.90 发布
- GoFrame v1.12 发布,数据库驱动开发、日志滚动切分等等新特性
- .NET Core 3.0 中的数据库驱动框架 System.Data
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Learning Vue.js 2
Olga Filipova / Packt Publishing / 2017-1-5 / USD 41.99
About This Book Learn how to propagate DOM changes across the website without writing extensive jQuery callbacks code.Learn how to achieve reactivity and easily compose views with Vue.js and unders......一起来看看 《Learning Vue.js 2》 这本书的介绍吧!