Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Golang use Oracle database on Ubuntu 14.04     - Physical structure and process disk IO (Linux)

- ORA-14400: inserted partition key does not map to any partition (Database)

- Mahout source code analysis: FP-Growth algorithm parallelization (Programming)

- Zabbix system email alert Python script (Server)

- RPM package creation Getting Started (Linux)

- The Sublime Text 3 configuration file (Linux)

- DupeGuru- find and remove duplicate files (Linux)

- How to restart after a crash Cinnamon (Linux)

- C ++ why we chose to use the smart pointer (Programming)

- Ubuntu 14.04 / 14.10 how to install FFmpeg 2.5.1 (Linux)

- Father of Python: Why Python zero-based index (Programming)

- Linux compiler of GCC (Linux)

- Getting Started with Linux system to learn: how to install the Shrew Soft IPsec VPN on Linux (Linux)

- DBCA Error: ORA-19809: limit exceeded for recovery files process (Database)

- Ubuntu 15.04 using the Eclipse 4.4, Java 8 and WTP (Linux)

- Linux command line under HTTP traffic sniffing tool: httpry (Linux)

- Gitolite how to import other Git repositories (Server)

- CentOS installation pycurl (Linux)

- Installation Mesos + Marathon + Zookeeper under CentOS 7 (Server)

- Go powerful development server simple example (Server)

 
         
  Golang use Oracle database on Ubuntu 14.04
     
  Add Date : 2016-12-24      
         
       
         
  This reference to the following address below:
 
https://github.com/Centny/Centny/blob/master/Articles/How%20build%20github.com%3amattn%3ago-oci8.md

First, install the Oracle OCI Suite

1, OCI download link page to download (instantclient-basic, instantclient-sdk)
 
http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
 
2, unzip to the same directory, such as: instantclient_12_1

3, root authority to move a folder to the directory / usr / lib

2, root permission to execute the following command

      In fact, over the past ## direct cp copy is 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
 
      ## The following two commands to run sqlplus
 
      ln / usr / lib / instantclient_12_1 / libsqlplusic.so /usr/lib/libsqlplusic.so
 

      ln /usr/lib/instantclient_12_1/libsqlplus.so /usr/lib/libsqlplus.so
 
        OCI path ## to the path added to the system to load dynamic library, and reload time
        echo / opt / oracle / instantclient >> /etc/ld.so.conf
        ldconfig
  
  3, install pkg-config
  
  4, create a file oci8.pc in / usr / lib / pkgconfig directory, as follows:
  
                prefix = < replace instantclient path>
 // Path to / usr / lib / instantclient_12_1
              libdir = $ {prefix}
              includedir = $ {prefix} / sdk / include /
 
              Name: OCI
              Description: Oracle database engine
              Version: 12.1
 // Version to actual version number
              Libs: -L $ {libdir} -lclntsh
              Libs.private:
              Cflags: -I $ {includedir}
 

5, step 6 will be reported directly run libaio error does not exist, the installation libaio library

sudo apt-get install libaio1

6, the installation go-oci8

go get github.com/mattn/go-oci8

7, .bashrc file, add the system variable

# 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 to add dynamic library loading path
export LD_LIBRARY_PATH = $ ORACLE_HOME
Path # oci8.pc file
export PKG_CONFIG_PATH = / usr / lib / pkgconfig

8, the contents of the 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)
    )
  )

Second, the use of Oracle database operation github.com/mattn/go-oci8
 
2.1 CRUD

package main
 
import (
 "Database / sql"
 "Fmt"
 _ "Github.com/mattn/go-oci8"
 "Os"
 )
 
func main () {
 
// character set
 os.Setenv ( "NLS_LANG", "AMERICAN_AMERICA.AL32UTF8")
 
// Note that the wording of the connection string
 db, err: = sql.Open ( "oci8", "awsdb/awsdb@192.168.0.126: 1521 / awsdb")
 if err! = nil {
 fmt.Println (err)
 return
 }
 // Open Transaction
 myTx, err: = db.Begin ()
 
myTx.Commit ()
 
// Query and traverse results
 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
 }
 // Close the database connection
 db.Close ()
 
}
 
2.2, execute a stored procedure, use the Save Results Structure
 
package main
 
import (
 "Database / sql"
 _ "Github.com/mattn/go-oci8"
 "Fmt"
 "Os"
 "Sync"
 )
 
var (
 db * sql.DB
 mux sync.Mutex
 )
 
// Define multi-line strings
 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 (20) NOT NULL, created VARCHAR (20) 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 people' 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 will not change the string error, but I created a database is of type int
 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 ()
 }
     
         
       
         
  More:      
 
- Copy and paste in Linux terminal and Vim (Linux)
- Linux character device - user mode and kernel mode data transfer data (Linux)
- The Linux disguised as windows to make the system more secure (Linux)
- Linux memory management (Linux)
- To install Docker under CentOS7 (Linux)
- Linux system security knowledge (Linux)
- Setting Squid successful anti-hotlinking (Linux)
- Ceph Source Analysis: Network Module (Server)
- ntop monitoring software configuration and installation (Linux)
- CentOS6.0 successful installation and configuration OpenCV (Linux)
- Atlassian Linux Shell Scripting the confluence remote backup methods (Linux)
- Security enhancements in Ubuntu ssh service (Linux)
- To install Oracle Database Details and FAQ Summary under CentOS (Database)
- Ubuntu and derivative system users how to install Pinta 1.5 (Linux)
- Java uses JDBC connect database (Programming)
- Java collections series (Programming)
- Linux installation JDK1.6 rpm.bin assembly (Linux)
- Talk about jsonp (Programming)
- Rails project prepared some tips small experience (Linux)
- MySQL Statistics (Database)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.