xorm oracle,Golang + Oracle 11g + XORM + go-oci8 的数据库驱动测试(超详细)
一、系統(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版本會(huì)報(bào)錯(cuò),改用 OCI 12.2.0.1.0 及 SDK 12.1.0.2.0就可以了。
二、安裝MinGW,注意安裝系統(tǒng)相應(yīng)的32/64位的版本
1、下載地址:
2、安裝MinGW
雙擊安裝程序
雙擊安裝程序
修改Architecture為:x86_64,點(diǎn)擊 Next > 直到安裝完成。注意:安裝路徑,后面需要用到
點(diǎn)擊 Next >
安裝完成目錄結(jié)構(gòu)
安裝完成
設(shè)置環(huán)境變量Path,添加:D:\Tool\MinGW64\mingw64\bin
設(shè)置環(huán)境變量
復(fù)制位置
三、下載Oracle 的 OCI和SDK
1、下載OCI和SDK
下載OCI和SDK文件
2、創(chuàng)建存放目錄,如:C:\instantclient_11_2,并將上面的兩個(gè)壓縮文件解壓到C:\instantclient_11_2中,如圖:
解壓的OCI和SDK
設(shè)置環(huán)境變量Path,添加:C:\instantclient_11_2
設(shè)置環(huán)境變量
在D:\Tool\MinGW64\mingw64\lib\pkg-config目錄下新建:oci8.pc文件,pkg-config目錄也需要新建。注意修改你的OCI目錄地址
新建的目錄結(jié)構(gòu)
目錄結(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
image.png
四、數(shù)據(jù)操作測(cè)試
測(cè)試文件main_test.go,測(cè)試前注意先添加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ū)動(dòng)
var dataSourceName = "ggs/123456@127.0.0.1:1521/ORCL" //數(shù)據(jù)庫(kù)賬號(hào):ggs,密碼:123456,實(shí)例服務(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測(cè)試
=== 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測(cè)試
API server listening at: 127.0.0.1:63941
=== RUN TestMattnOracle
1 張三
2 李四
--- PASS: TestMattnOracle (0.06s)
PASS
Debugger finished with exit code 2
五、錯(cuò)誤問(wèn)題及解決方案
1、OCI版本不一致,報(bào)錯(cuò)的相關(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
錯(cuò)誤原因:OCI版本問(wèn)題,我是在Oracle 11g版本下使用 OCI 及 SDK 11.2.0.4.0版本會(huì)報(bào)錯(cuò)
解決方法:改用 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.
錯(cuò)誤原因: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}
總結(jié)
以上是生活随笔為你收集整理的xorm oracle,Golang + Oracle 11g + XORM + go-oci8 的数据库驱动测试(超详细)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python爬虫实战,爬取洛杉矶上千房源
- 下一篇: PEAKCOO做数显压力表芯片的方案设计