如何在Go中连接到Oracle数据库

huangapple go评论90阅读模式
英文:

How to connect to Oracle in go

问题

我了解到在Go语言中连接Oracle数据库有两种方式(在Windows上):

  1. github.com/tgulacsi/goracle
  2. github.com/mattn/go-oci8

但对于我这样的水平(刚入门开源+golang),这两种方法/驱动都非常棘手。

而且在部署、在不同机器上进行开发等方面也是一种负担(还要假设它能正常工作)。

是否有更好的方法来在golang中连接Oracle数据库,如果没有的话,是否有人可以用高层次的视角或任何视角来解释一下,使这个过程更容易一些?

非常感谢指点。

英文:

I gather there are two ways to connect to Oracle DB in Go (on windows):

  1. github.com/tgulacsi/goracle
  2. github.com/mattn/go-oci8

But for someone of my level (beginner in open source+golang), those two methods/drivers are awfully tricky.

It's also a burden having to go through all of that for deployment, development on different machines etc. (Also assuming it will work).

Is there a better way to connect to Oracle db in golang or if there is not then can someone explain to me in high level view or any view for that matter that would make this easier?

Pointers would be very much appreciated.

TQ.

答案1

得分: 9

如果你还感兴趣的话,我最近几个月一直在使用Go和Oracle在Windows上工作。到目前为止,我最喜欢的驱动程序是go-oci8。它比goracle快得多,而且似乎更活跃。

我们的一些应用程序需要部署在我们无法访问的计算机上。这两个本地SQL驱动程序都是与应用程序一起编译的,不需要任何外部配置,这是一个巨大的优点。计算机仍然需要安装Oracle客户端,但这是唯一的外部依赖。

我不会说go-oci8已经完全适用于生产环境,但是当你了解它的限制时,它已经足够稳定了。一个例子是当同时在多个goroutine上运行时,它会发生panic,所以如果你需要这个功能,你可能需要使用互斥锁。

我基本上按照这个教程来安装它:https://gist.github.com/mnadel/8678269

最棘手的部分是正确创建oci8.pc文件。我的文件内容如下:

prefix=/devel/target/1.0
exec_prefix=${prefix}
libdir=C:/oracle/instantclient_12_1_64/sdk/lib/msvc
includedir=C:/oracle/instantclient_12_1_64/sdk/include
oralib=C:/oracle/instantclient_12_1_64/sdk/lib/msvc
orainclude=C:/oracle/instantclient_12_1_64/sdk/include
gcclib=c:/MinGW_64/mingw64/lib
gccinclude=c:/MinGW_64/mingw64/lib
glib_genmarshal=glib-genmarshal
gobject_query=gobject-query
glib_mkenums=glib-mkenums
Name: oci8
Version: 12.1
Description: oci8 library
Libs: -L${oralib} -L${gcclib} -loci
Libs.private:
Cflags: -I${orainclude} -I${gccinclude}

有些内容可能是多余的,我可能会在一个干净的机器上进行改进。

需要注意的一点是,你应该在Go和Oracle客户端中使用相同的架构。所以如果你想使用64位版本的Go,你也需要64位版本的Oracle。我同时拥有32位和64位版本的两者,虽然64位是我的默认版本,但当我需要构建32位版本时,我使用批处理文件来更改必要的路径和环境变量。

投入一些时间使其正常工作可能是值得的,你可能会比使用ODBC获得更好的性能。我一直在处理相当大量的数据(查询获取500万行以上),它的表现非常好。

英文:

If you are still interested, I have been working with Go and Oracle on Windows for a few months now. My favorite driver so far is go-oci8. It is much faster than goracle and seems to be more active.

Some of our applications need to be deployed on computers that we don't have access to. Both native SQL drivers are compiled with the application without the need for any external configuration, so that is a huge plus. The computer will still need Oracle client installed, but that is the only external dependency.

I won't say go-oci8 is production ready yet, but it's stable enough when you know its limitations. One example is that it panics when running on multiple goroutines simultaneously, so if you need that you might want to use a mutex.

I have basically followed this tutorial to install it: https://gist.github.com/mnadel/8678269

The trickiest part was creating oci8.pc corretly. Mine is:

prefix=/devel/target/1.0
exec_prefix=${prefix}
libdir=C:/oracle/instantclient_12_1_64/sdk/lib/msvc
includedir=C:/oracle/instantclient_12_1_64/sdk/include
oralib=C:/oracle/instantclient_12_1_64/sdk/lib/msvc
orainclude=C:/oracle/instantclient_12_1_64/sdk/include
gcclib=c:/MinGW_64/mingw64/lib
gccinclude=c:/MinGW_64/mingw64/lib
glib_genmarshal=glib-genmarshal
gobject_query=gobject-query
glib_mkenums=glib-mkenums
Name: oci8
Version: 12.1
Description: oci8 library
Libs: -L${oralib} -L${gcclib} -loci
Libs.private:
Cflags: -I${orainclude} -I${gccinclude}

Some things might be reduntant, I might try to improve it on a clean machine.

An important thing to have in mind is that you should use the same architecture for Go and the Oracle client. So if you want to use the 64 bit version of Go you will also need the 64 bit version of Oracle. I have both 32 and 64 bit versions of both, and while 64 bit is my default I use bat files to change the necessary paths and environment variables when I need to build a 32 bit version.

It might be worth investing some time to make it work, you will probably get much better performance than using ODBC. I have been using it with somewhat high data volume (queries that fetch 5+ million rows) and it works very well.

huangapple
  • 本文由 发表于 2014年3月6日 15:16:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/22217665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定