Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Golang use Oracle database on Ubuntu 14.04     - Cobbler batch install Ubuntu / CentOS system (Linux)

- Ten to improve the efficiency of the Linux bash tricks (Linux)

- Use scripts easily install the latest Linux kernel in Ubuntu (Linux)

- About Git (Linux)

- How to adjust the system time CentOS (Linux)

- Linux keyboard recording script (Linux)

- Android HTTP request with Get Information (Programming)

- CentOS network configuration 7, and set the host name and IP-bound problems (Linux)

- CentOS 7 open ports (Linux)

- Linux command line ten magic usage (Linux)

- Cobbler remotely install CentOS system (Linux)

- Puppet installation and testing (Server)

- RedHat install GCC problem --- Depends (Linux)

- Rails project prepared some tips small experience (Linux)

- Install Ubuntu open source drawing program MyPaint 1.2.0 (Linux)

- Thinking in Java study notes - Generics (Programming)

- Linux system performance tuning of Analysis (Linux)

- ORA-28000 the account is locked fault simulation (Database)

- CentOS 7 Change Hostname (Linux)

- Android Unzip the ZIP / GZIP data (based on the InflaterInputStream implementation) (Programming)

 
         
  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:      
 
- Linux upgrade GCC 4.8.1 clear and concise tutorials (Ubuntu 12.04 64-bit version as an example) (Linux)
- Examples of safety testing system notes for RedHat Linux (Linux)
- TCP network communication Java Programming (Programming)
- Oracle to read and modify the data block process (Database)
- Linux operating system security settings initial understanding (Linux)
- Hadoop connection failed or stuck processing (Server)
- Ten best plug surge Emacs Productivity (Linux)
- Linux keyboard recording script (Linux)
- Configuring Proxy on a Unix terminal, accelerate Android Studio Construction (Linux)
- Java recognize simple codes (Programming)
- Nginx DHCP TFTP Kickstart set up automatic installation system (Server)
- How to install PlayOnLinux 4.2.5 under Ubuntu 14.04 / 12.04 (Linux)
- Linux operating system security settings initial understanding (Linux)
- PHP with FastCGI and mod_php Comments (Server)
- Linux foundation tutorial: how to modify the host name on CentOS or RHEL 7 (Linux)
- Ubuntu installed racing game Speed Dreams 2.1 (Linux)
- Ubuntu 12.04 kernel configuration automatically restart and crash dump (Linux)
- Redis master-slave replication switch (Database)
- How to cool down your Ubuntu system (Linux)
- Android basics summary article (Programming)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.