This is a creation in Article, where the information may have evolved or changed. I. Installation of the OCI Kit for Oracle
1, oci download link page download (INSTANTCLIENT-BASIC,INSTANTCLIENT-SDK)
Http://www.oracle.com/technetwor ... t/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 permission to execute the following command
# # 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.so
ln/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=
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 install directory join dynamic library load Path
Export Ld_library_path= $ORACLE _home
# oci8.pc file is located on the path
Export Pkg_config_path=/usr/lib/pkgconfig
8. Contents of Tnsnames.ora File
code: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
Code:package Main
Import ( "Database/sql" "FMT" _ "Github.com/mattn/go-oci8" "OS" )
Func Main () {
Character 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 (&F1, &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 () } |