Golang Connection Orcale

Source: Internet
Author: User
Tags stmt
This is a creation in Article, where the information may have evolved or changed. The use of Glang for some time, the first actually did not like his grammar, but later familiar with the found that it is quite cool. Before the database has been using MySQL, connected to no problem, GitHub has a lot of perfect drivers, so that the connection to other databases should be no problem, recently heard that its connection Orcale more trouble, so they tried.

The reason why the connection Orcale is troublesome is because Orcale does not provide the driver of Golang, so it can not be connected like Java, nor can it be driven by the open source database like MySQL. However, is so-called despair, since the pure go can not be realized, then also use CGO implementation, C language should always support it.
There are many methods on the Internet, but they are all implemented using OCI, where we use Go-oci8 (https://github.com/wendal/go-oci8)

First, installation MinGW

First we configure the environment, because the CGO need GCC support, so need to configure the GCC environment, if it is generally possible to skip this step under Linux, win will need to install their own, here we use MinGW , can go to the official website now download, but the official website directly provided by the installation program needs to install the network downloads, it is likely to fail, so after several searches to find the following URL:
    • MinGW Download address Related:http://blog.csdn.net/mecho/article/details/24305369
the inside of the detailed introduction MinGW Each version of the different, can be downloaded according to their own situation, because I am 64-bit, here to download of 64-bit POSIX version .
the specific installation process is not crap, directly decompression can be.

ii. installation of OCI

since there is no corresponding driver package, we cannot connect directly, so we need to provide the OCI interface via Orcale (a bit like ODBC), which contains the required class library. To download the direct website, find the corresponding version of the system:
    • OCI Download Address: http://www.oracle . com/technetwork/topics/winsoft-085727.html
need to download Basic and SDK, unzip the basic to instantclient_11_1 after downloading , then unzip the SDK to Under the INSTANTCLIENT_11_1\SDK.
in the instantclient_11_1 Build the \network\admin\ directory, add Tnsnames.ora (This does not explain it), the content according to their own orcale settings.

Third, configuration Go-oci8

Direct Go get github.com/wendal/go-oci8 (Error free), and then to Go\src\github.com/wendal\go-oci8\windows, will pkg-config.exe Copy toMinGWunder \ Bin, set oci8.pc Copy to Mingw\lib\pkg-config\ , and editoci8.pc:
# Information for pkg-configprefix= modified to instantclient_11_1 directory, such as f:/dev/instantclient_11_1exec_prefix= Modify to Instantclient_11_1 directory, such as F:/dev/instantclient_11_1libdir=${exec_prefix}includedir=${prefix}/sdk/include/name: Ocidescription:oracle database engineversion:11.2libs:-l${libdir}-locilibs.private:cflags:-I${includedir}

iv. Setting Environment variables

environment variable under path add Instantclient_11_1 and Path of the Mingw\bin
Add Pkg_config_path=c:\mingw\lib\pkg-config
add tns_admin= F:\dev\instantclient_11_1 \ network\admin\

v. Testing

Execute hereGo getGithub.com/wendal/go-oci8, if no Cuona instructions are done. For specific test code, seeGithub.com\wendal\go-oci8\example under theOracle.go, pay attention to the inside of the importMATTN changed intoWendal. Here is my code:
package mainimport ("Database/sql" _ "github.com/wendal/ Go-oci8 "" Log ") func query () {//For log add short name, easy to see the number of rows log. SetFlags (log. Lshortfile | Log. lstdflags)//username/password @ instance name with Sqlplus conn command similar to DB, err: = SQL. Open ("Oci8", "USERNAME/PWD@ORCL") if err! = Nil {log. Fatal (ERR)}defer db. Close () rows, err: = db. Query ("Select name from Fub_b") if err! = Nil {log. Fatal (Err)}for rows. Next () {var name stringrows. Scan (&name) log. Printf ("name =%s, len=%d", name, Len (name))}rows. Close ()}func update () {//Add a short name for log to make it easier to see the number of rows log. SetFlags (log. Lshortfile | Log. lstdflags)//username/password @ instance name with Sqlplus conn command similar to DB, err: = SQL. Open ("Oci8", "USERNAME/PWD@ORCL") if err! = Nil {log. Fatal (ERR)}defer db. Close () stmt, _: = db. Prepare (' UPDATE fub_b set name = ' CNM ') result, err: = stmt. Exec () if err! = Nil {log. Fatal (Err)}count, _: = result. rowsaffected () log. Printf ("Result count:%d", count)}func main () {update ()} 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.