- Golang + Oracle 11g + XORM + go-oci8 的數(shù)據(jù)庫驅(qū)動測試(超詳細(xì))
- Golang + Oracle 11g + GORM(改) + go-oci8 的數(shù)據(jù)庫驅(qū)動測試(超詳細(xì))
一、系統(tǒng)環(huán)境介紹:
- 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炊昆,注意安裝系統(tǒng)相應(yīng)的32/64位的版本
1、下載地址:
https://sourceforge.net/projects/mingw-w64/files/
2威根、安裝MinGW
-
雙擊安裝程序
- 修改Architecture為:
x86_64
凤巨,點(diǎn)擊Next >
直到安裝完成。注意:安裝路徑洛搀,后面需要用到
-
安裝完成目錄結(jié)構(gòu)
- 設(shè)置環(huán)境變量Path敢茁,添加:
D:\Tool\MinGW64\mingw64\bin
- 下載
pkg-config.exe
,并復(fù)制到D:\Tool\MinGW64\mingw64\bin
目錄下
下載地址:https://raw.githubusercontent.com/wendal/go-oci8/master/windows/pkg-config.exe
項目開源地址:https://github.com/wendal/go-oci8
復(fù)制后如圖:
三留美、下載Oracle 的 OCI和SDK
1卷要、下載OCI和SDK
https://www.oracle.com/technetwork/topics/winx64soft-089540.html
2、創(chuàng)建存放目錄独榴,如:C:\instantclient_11_2
,并將上面的兩個壓縮文件解壓到C:\instantclient_11_2
中奕枝,如圖:
-
設(shè)置環(huán)境變量Path棺榔,添加:
C:\instantclient_11_2
-
在
D:\Tool\MinGW64\mingw64\lib\pkg-config
目錄下新建:oci8.pc
文件,pkg-config
目錄也需要新建隘道。注意修改你的OCI目錄地址
新建的目錄結(jié)構(gòu)
修改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}
設(shè)置環(huán)境變量PKG_CONFIG_PATH
四症歇、數(shù)據(jù)操作測試
測試文件main_test.go
,測試前注意先添加userinfo
表及數(shù)據(jù)
package main_test
import (
"database/sql"
"testing"
"github.com/go-xorm/xorm"
_ "github.com/mattn/go-oci8"
)
var driverName = "oci8" //Oracle 驅(qū)動
var dataSourceName = "ggs/123456@127.0.0.1:1521/ORCL" //數(shù)據(jù)庫賬號:ggs谭梗,密碼:123456忘晤,實例服務(wù):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版本不一致远舅,報錯的相關(guān)內(nèi)容:
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}