Go语言(GO lang)连接Oracle Database使用godror
GO lang通用的、跨平台的、开源的编程语言,(Started at Google in 2007)GoLANG在高并发支持非常优秀,相比python更快,godror使用ODPI-C(用于C的Oracle数据库编程接口)为Oracle数据库实现了Go数据库/sql驱动程序, 在中文支持方面非常不错, 这里记录Go连接oracle数据库的方法, GO for Windows 开发环境配置.
1,GO 安装
— for linux
[root@localhost ~]# wget https://golang.google.cn/dl/go1.15.6.linux-amd64.tar.gz --2020-12-18 19:43:59-- https://golang.google.cn/dl/go1.15.6.linux-amd64.tar.gz Resolving golang.google.cn (golang.google.cn)... 203.208.41.34 Connecting to golang.google.cn (golang.google.cn)|203.208.41.34|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://dl.google.com/go/go1.15.6.linux-amd64.tar.gz [following] --2020-12-18 19:43:59-- https://dl.google.com/go/go1.15.6.linux-amd64.tar.gz Resolving dl.google.com (dl.google.com)... 203.208.41.65 Connecting to dl.google.com (dl.google.com)|203.208.41.65|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 120951514 (115M) [application/octet-stream] Saving to: ‘go1.15.6.linux-amd64.tar.gz’ 100%[====================================================================================================================>] 120,951,514 7.70MB/s in 16s 2020-12-18 19:44:16 (7.12 MB/s) - ‘go1.15.6.linux-amd64.tar.gz’ saved [120951514/120951514] [root@localhost ~]# tar -C /usr/local -xzf go1.15.6.linux-amd64.tar.gz [root@localhost ~]# vi .bash_profile export PATH=$PATH:/usr/local/go/bin [root@localhost ~]# . .bash_profile [root@localhost ~]# which go /usr/local/go/bin/go [root@localhost ~]# go version go version go1.15.6 linux/amd64
— for Windows 10
下载
https://golang.google.cn/doc/install?download=go1.15.6.windows-amd64.msi
C:\Users\zhang>go version go version go1.15.6 windows/amd64
2, 安装Oracle Instant Client
虽然Oracle客户端库不需要编译,他们都 在运行时需要的。从https://www.oracle.com/database/technologies/instant-client/downloads.html下载免费的Basic或Basic Light软件包 。
# oracle basic client
https://download.oracle.com/otn_software/nt/instantclient/19900/instantclient-basic-windows.x64-19.9.0.0.0dbru.zip
# oracle jdk client
https://download.oracle.com/otn_software/nt/instantclient/19900/instantclient-sdk-windows.x64-19.9.0.0.0dbru.zip
合并到D:\orainstantclient_19_9, 改变PATH=D:\orainstantclient_19_9;$PATH 环境变量
/drives/d/orainstantclient_19_9 ls -l total 118940 -r-xr-x--- 1 weejar UsersGrp 5903 Dec 19 09:30 BASIC_LICENSE -r-xr-x--- 1 weejar UsersGrp 1725 Dec 19 09:30 BASIC_README -r-xr-x--- 1 weejar UsersGrp 1238 Dec 19 09:31 SDK_LICENSE -r-xr-x--- 1 weejar UsersGrp 5903 Dec 19 09:31 SDK_README -r-xr-x--- 1 weejar UsersGrp 28672 Dec 19 09:30 adrci.exe -r-xr-x--- 1 weejar UsersGrp 38496 Dec 19 09:30 adrci.sym -r-xr-x--- 1 weejar UsersGrp 72704 Dec 19 09:30 genezi.exe -r-xr-x--- 1 weejar UsersGrp 70112 Dec 19 09:30 genezi.sym -r-xr-x--- 1 weejar UsersGrp 807424 Dec 19 09:30 oci.dll -r-xr-x--- 1 weejar UsersGrp 784832 Dec 19 09:30 oci.sym -r-xr-x--- 1 weejar UsersGrp 182272 Dec 19 09:30 ocijdbc19.dll -r-xr-x--- 1 weejar UsersGrp 57264 Dec 19 09:30 ocijdbc19.sym -r-xr-x--- 1 weejar UsersGrp 610816 Dec 19 09:30 ociw32.dll -r-xr-x--- 1 weejar UsersGrp 111208 Dec 19 09:30 ociw32.sym -r-xr-x--- 1 weejar UsersGrp 4406794 Dec 19 09:30 ojdbc8.jar -r-xr-x--- 1 weejar UsersGrp 88576 Dec 19 09:30 oramysql19.dll -r-xr-x--- 1 weejar UsersGrp 55824 Dec 19 09:30 oramysql19.sym -r-xr-x--- 1 weejar UsersGrp 4761600 Dec 19 09:30 orannzsbb19.dll -r-xr-x--- 1 weejar UsersGrp 2353504 Dec 19 09:30 orannzsbb19.sym -r-xr-x--- 1 weejar UsersGrp 1178112 Dec 19 09:30 oraocci19.dll -r-xr-x--- 1 weejar UsersGrp 1374224 Dec 19 09:30 oraocci19.sym -r-xr-x--- 1 weejar UsersGrp 1202176 Dec 19 09:30 oraocci19d.dll -r-xr-x--- 1 weejar UsersGrp 1314040 Dec 19 09:30 oraocci19d.sym -r-xr-x--- 1 weejar UsersGrp 206456320 Dec 19 09:30 oraociei19.dll -r-xr-x--- 1 weejar UsersGrp 15138072 Dec 19 09:30 oraociei19.sym -r-xr-x--- 1 weejar UsersGrp 288768 Dec 19 09:30 oraons.dll -r-xr-x--- 1 weejar UsersGrp 236544 Dec 19 09:30 orasql19.dll -r-xr-x--- 1 weejar UsersGrp 65400 Dec 19 09:30 orasql19.sym dr-xr-x--- 1 weejar UsersGrp 0 Dec 19 09:31 sdk -r-xr-x--- 1 weejar UsersGrp 1686472 Dec 19 09:30 ucp.jar -r-xr-x--- 1 weejar UsersGrp 28672 Dec 19 09:30 uidrvci.exe -r-xr-x--- 1 weejar UsersGrp 38496 Dec 19 09:30 uidrvci.sym dr-xr-x--- 1 weejar UsersGrp 0 Dec 19 09:30 vc14 -r-xr-x--- 1 weejar UsersGrp 74263 Dec 19 09:30 xstreams.jar
3,安装 mingw-w64
Windows上的GCC编译. Note that Windows may need some newer gcc (mingw-w64 with gcc 7.2.0).
下载
https://sourceforge.net/projects/mingw-w64/files/ 选择x86_64-posix-seh 如:选择8.1 posix-seh
下载后解压到如D:\mingw64, PATH环境变量增加 D:\mingw64\bin。
4,安装Godror驱动程序
按照GitHub上goracle存储库中的说明进行操作:
$ go get github.com/godror/godror
等待成功后无输出, 用户手册 https://godror.github.io/godror/doc/installation.html
5, 准备oracle database
这里使用VM中的 Oracle 19c。
6,GO代码连接Oracle数据库
package main import ( "fmt" "database/sql" _ "github.com/godror/godror" ) const ( host = "192.168.56.102" port = 1521 user = "anbob" sqlpassword = "anbob" dbname = "pdb1" ) func main(){ // 用户名/密码@IP:端口/实例名 oralInfo := fmt.Sprintf("%s/%s@%s:%d/%s", user, sqlpassword, host, port, dbname) fmt.Println(oralInfo) db, err := sql.Open("godror", oralInfo) //db, err := sql.Open("godror", "anbob/anbob@192.168.56.102/pdb1") fmt.Println("连接成功!") err = db.Ping() if err != nil { fmt.Println(err) return } defer db.Close() rows,err := db.Query("select banner from v$version") if err != nil { fmt.Println("Error running query") fmt.Println(err) return } cols, _ := rows.Columns() fmt.Printf("Result columns : %s\n", cols) defer rows.Close() var dbVersion string for rows.Next() { rows.Scan(&dbVersion) } fmt.Printf("数据库版本 : %s\n", dbVersion) }
执行
D:\code\gotest>go run connectora.go anbob/anbob@192.168.56.102:1521/pdb1 连接成功! Result columns : [BANNER] 数据库版本 : Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production D:\code\gotest> D:\code\gotest>go build connectora.go D:\code\gotest>connectora.exe anbob/anbob@192.168.56.102:1521/pdb1 连接成功! Result columns : [BANNER] 数据库版本 : Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
— enjoy —
对不起,这篇文章暂时关闭评论。