Golang using the Oracle database on UBUNTU 14.04

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. This article refers to an article with the following address: https://github.com/Centny/Centny/blob/master/Articles/How%20build%20github.com%3amattn%3ago-oci8.md

I. Installation of the OCI Kit for Oracle
1, oci download link page download (INSTANTCLIENT-BASIC,INSTANTCLIENT-SDK)

Http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html

2, unzip to the same directory, for example: Instantclient_12_1


3, root permissions to move the folder to the directory/usr/lib


2, root permissions to perform the following life

# # actually the direct CP copy used to be the same
Ln/usr/lib/instantclient_12_1/libclntsh.so.12.1/usr/lib/libclntsh.so
Ln/usr/lib/instantclient_12_1/libocci.so.12.1/usr/lib/libocci.so
Ln/usr/lib/instantclient_12_1/libociei.so/usr/lib/libociei.so
Ln/usr/lib/instantclient_12_1/libnnz12.so/usr/lib/libnnz12.so # # Two below is to run the Sqlplus command ln/usr/lib/instantclient_12_1/libsqlplusic.so/usr/lib/libsqlplusic.soLn/usr/lib/instantclient_12_1/libsqlplus.so/usr/lib/libsqlplus.so
# # Add the OCI path to the system to load the path of the dynamic library and reload it once
       Echo/opt/oracle/instantclient >>/etc/ld.so.conf
     Ldconfig

3, Installation Pkg-config

4, in the/usr/lib/pkgconfig directory to create the file oci8.pc, the contents are as follows:

Prefix=<replace instantclient path> Path changed to/usr/lib/instantclient_12_1
Libdir=${prefix}
includedir=${prefix}/sdk/include/

Name:oci
Description:oracle Database Engine
version:12.1 Version changed to actual version number
Libs:-l${libdir}-lclntsh
Libs.private:
Cflags:-i${includedir}

5, directly run Step 6 will report Libaio error, install Libaio Library
sudo apt-get install Libaio1
6, Installation Go-oci8

Go get github.com/mattn/go-oci8
7. Add system variables to the. bashrc file
# OCI installation directory
Export Oracle_home=/usr/lib/instantclient_12_1 # Tnsnames.ora file Address
Export tns_admin= $ORACLE _home/network/admin # OCI installation directory join the dynamic library loading path
Export ld_library_path= $ORACLE _home # oci8.pc file path
Export Pkg_config_path=/usr/lib/pkgconfig
8. Contents of Tnsnames.ora File
awsdb=
(DESCRIPTION =
(Address_list =
(ADDRESS = (PROTOCOL = TCP) (HOST = 192.168.0.126) (PORT = 1521))
)
(Connect_data =
(service_name = awsdb)
)
)


Extproc_connection_data =
(DESCRIPTION =
(Address_list =
(ADDRESS = (PROTOCOL = IPC) (KEY = EXTPROC1))
)
(Connect_data =
(SID = Plsextproc)
(PRESENTATION = RO)
)
)
Ii. using GITHUB.COM/MATTN/GO-OCI8 to operate the Oracle database
2.1, increase and revise the search
Package Main


Import (
"Database/sql"
"FMT"
_ "Github.com/mattn/go-oci8"
"OS"
)


Func Main () { //Character Set Os. Setenv ("Nls_lang", "American_america". Al32utf8 ")

Note the notation of the connection string
DB, err: = SQL. Open ("Oci8", "awsdb/awsdb@192.168.0.126:1521/awsdb")
If err! = Nil {
Fmt. PRINTLN (ERR)
Return
}
Transaction Open
Mytx,err:=db. Begin ()

Mytx.commit ()
Query and result traversal Rows, err: = db. Query ("Select Nodeid,nodename from Aa_mt_test")
If err! = Nil {
Fmt. PRINTLN (ERR)
Return
}
For rows. Next () {
var F1 string
var F2 string
Rows. Scan (&AMP;F1, &AMP;F2)
println (f1, F2)//3.14 foo
}
Rows. Close ()

_, Err = db. Exec ("CREATE table foo (bar VARCHAR2 (256))")
_, Err = db. Exec ("drop table foo")
If err! = Nil {
Fmt. PRINTLN (ERR)
Return
}
To close a database connection
Db. Close ()
}
2.2, executed a stored procedure, using the structure to save the results
Package Main


Import (
"Database/sql"
_ "Github.com/mattn/go-oci8"
"FMT"
"OS"
"Sync"
)


VAR (
DB *sql. Db
Mux Sync. Mutex
)

//definition of multi-line string
var usertablesql string = '
BEGIN
BEGIN
EXECUTE IMMEDIATE ' DROP TABLE user_profile ';
EXCEPTION
When OTHERS Then
IF SQLCODE! = -942 Then
RAISE;
END IF;
END;
EXECUTE IMMEDIATE ' CREATE TABLE user_profile (id int PRIMARY KEY, name VARCHAR () not NULL, created VARCHAR (a) NOT null) ';
END;
`


func init () { //Lock Mux. Lock ()
Defer MUX. Unlock ()


Os. Setenv ("Nls_lang", "American_america". ZHS16GBK ")
Check
If db! = Nil {
Return
}


Open
OracleDB, err: = SQL. Open ("Oci8", "Awsdb/awsdb@192.168.0.126:1521/awsdb")
Checkerr (ERR)


New DB
db = OracleDB


Create DATABASE table
_, Err = db. Exec (Usertablesql)
Checkerr (ERR)
}


Func Checkerr (err error) {
If err! = Nil {
Panic ("Oracle ERR:" + err.) Error ())
}
Return
}


Func Main () {
Insert
Insertsql: = ' insert into User_profile (id,name,created) VALUES (1, ' Viney ', ' 2013-03-06 ') '
_, Err: = db. Exec (Insertsql)
Checkerr (ERR)


Update
Updatesql: = ' update user_profile set name= ' Chinese ' where id=1 '
_, Err = db. Exec (Updatesql)
Checkerr (ERR)


Select
Querysql: = ' select * from User_profile where id=1 '
Rows, err: = db. Query (Querysql)


Type user struct {
ID int//This place is changed to string to not error, but I create database is int type
Name string
Created string
}


var u = &user{}
For rows. Next () {
Err = rows. Scan (
&u.id,
&u.name,
&u.created)
Checkerr (ERR)
}
Rows. Close ()


Fmt. Println (*u)


Delete
Deletesql: = ' Delete from user_profile where id=1 '
_, Err = db. Exec (Deletesql)
Checkerr (ERR)


Db. Close ()
}









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.