windows 64位 golang oracle解决方案

其他待补充,或留言,这是我成功连通后才写的文章,可能有遗漏

我最讨厌,从不windows编译的人,都搞定了,相信我,你可以的。

中间来回穿插太多尝试,所以可能有些操作是多余的,自行精简。

一:oracle客户端和sdk

总下载列表:

https://www.oracle.com/technetwork/cn/database/features/instant-client/index-092699-zhs.html

windows x64下载列表

https://www.oracle.com/technetwork/cn/topics/winx64soft-101515-zhs.html

我下载了:instantclient-basic-windows.x64-12.2.0.1.0.zip和instantclient-sdk-windows.x64-12.2.0.1.0.zip

instantclient-basic-windows.x64-12.2.0.1.0.zip解压到一个目录,instantclient-sdk-windows.x64-12.2.0.1.0.zip解压到它目录下

二:mingw(解决:exec: "gcc": executable file not found in %PATH%)

扫描二维码关注公众号,回复: 6461589 查看本文章

64位下载地址(我用的这个):

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

添加path环境 E:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin (根据自己的情况定)

32位下载地址(不需要不用管):

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

三:常用go-oci8驱动

https://github.com/wendal/go-oci8

go get github.com/wendal/go-oci8

会爆一个错误,先不管它


四:找到环境变量里GOPATH的目录

这里我貌似还添加了一个path环境变量%GOPATH%\src\github.com\mattn\go-oci8 (具体有没有效果我忘了-、-你自己试试)

然后再这个GOPATH目下的\src\github.com\wendal\go-oci8目录

复制windows\pkg-config.exe到到mingw的安装目录mingw64\bin

复制windows\oci8.pc到到mingw的安装目录mingw64\lib\pkg-config的oci8.pc

oci8.pc可以照着我的改

# Package Information for pkg-config

prefix=D:/instantclient_10_2
exec_prefix=D:/instantclient_10_2
libdir=D:/instantclient_10_2/sdk/lib/msvc/
includedir=${prefix}/sdk/include/

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

执行报错

# github.com/wendal/go-oci8

D:\GOENV\src\github.com\wendal\go-oci8\oci8.go:119: cannot use (**_Ctype_struct_OCIServer)(unsafe.Pointer(&conn.svc)) (type **_Ctype_struct_OCIServer) as type **_Ctype_struct_OCISvcCtx in argument to func literal

D:\GOENV\src\github.com\wendal\go-oci8\oci8.go:136: cannot use (*_Ctype_struct_OCIServer)(c.svc) (type *_Ctype_struct_OCIServer) as type *_Ctype_struct_OCISvcCtx in argument to func literal

D:\GOENV\src\github.com\wendal\go-oci8\oci8.go:263: cannot use (*_Ctype_struct_OCIServer)(s.c.svc) (type *_Ctype_struct_OCIServer) as type *_Ctype_struct_OCISvcCtx in argument to func literal

D:\GOENV\src\github.com\wendal\go-oci8\oci8.go:383: cannot use (*_Ctype_struct_OCIServer)(s.c.svc) (type *_Ctype_struct_OCIServer) as type *_Ctype_struct_OCISvcCtx in argument to func literal

替换GOPATH目下的\src\github.com\wendal\go-oci8里四处OCIServer为OCISvcCtx。

编译,完美。

补充

1.可能需要添加环境变量(解决:exec: “pkg-config”: executable file not found in %PATH%)

PKG_CONFIG_PATH    E:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\lib\pkg-config

2.go oracle另两个驱动库 

github.com/go-goracle/goracle oci8库中文乱码

     db, err := sql.Open("goracle", "username/[email protected]:1521/long")

gopkg.in/rana/ora.v4 (这个库比较吊,可以解决密码特殊字符例如@)实测后,发现此库对scan Byte支持较差,这是硬伤

    db, err := sql.Open("ora", "username/\"123456#@!\"@112.99.194.54:1521/long")

github.com/mattn/go-oci8

    这个库windows下没安装的起来。

3.测试连通的代码

package main

import (
	"database/sql"
	"fmt"
	"log"
	"os"

	_ "github.com/wendal/go-oci8"
)

func query() {
	os.Setenv("NLS_LANG", "AMERICAN_AMERICA.AL32UTF8")
	log.SetFlags(log.Lshortfile | log.LstdFlags)

	db, err := sql.Open("oci8", "username/[email protected]:1521/long")

	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	rows, err := db.Query("select * from LF_INFO where id=10161")
	if err != nil {
		log.Fatal(err)
	}
	cols, _ := rows.Columns()

	rawResult := make([][]byte, len(cols))
	result := make([]string, len(cols))
	dest := make([]interface{}, len(cols))

	for i, _ := range rawResult {
		dest[i] = &rawResult[i]
	}

	if rows.Next() {

		err = rows.Scan(dest...)

		for i, raw := range rawResult {
			if raw == nil {
				result[i] = ""
			} else {
				result[i] = string(raw)
			}
		}

		fmt.Printf("%#v\n", result)
	}
	rows.Close()
}

func main() {
	query()
}

4.生成的exe移刀其他环境运行,是否需要重新安装客户端?(待测试,google到的

仅需要这三个文件,亲测

其他待补充,或留言,这是我成功连通后才写的文章,可能有遗漏

猜你喜欢

转载自blog.csdn.net/wq57885/article/details/82841792