Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Golang use Oracle database on Ubuntu 14.04     - C language print various graphic (Programming)

- Vim Common Command Summary (Linux)

- Linux / proc directory Comments (Linux)

- Repair after installing Ubuntu no boot device error (Linux)

- Detailed Linux network security policies and protection measures (Linux)

- Using Maven to download Spring (Linux)

- Use virt-manager to create and manage virtual machines (Linux)

- Fatal: unable to connect to github.com problem solving (Linux)

- Use Tmux and Vim to make IDE (Linux)

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

- Share and show your code on GitHub (Linux)

- To install PostgreSQL 9.4 (Database)

- Ubuntu 15.04 install Complete Guide (Linux)

- Binary Packages Golang (Linux)

- Ubuntu update bug fixes Daquan (Linux)

- True and false in Perl (Programming)

- QEMU code analysis: BIOS loading process (Linux)

- How to enable curl command HTTP2 support (Linux)

- Java and C / C ++ data conversion when network communication (Programming)

- Binary search is really easy as you think you do (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 fast set ip bond (Linux)
- Close common port to protect server security (Linux)
- Use OpenSSL carried BASE64 encoding and decoding (Linux)
- How to back up Debian system backupninja (Linux)
- Linux firewall settings -DNS server articles (Server)
- Beautiful start Ubuntu installation tool Duck Launcher 0.64.5 (Linux)
- Use NTFS-3G to mount NTFS partitions under Linux U disk and removable hard disk (Linux)
- Linux Getting Started Tutorial: Ubuntu laptop screen brightness adjustment (Linux)
- Setting Hibernate connection MySQL database connection pool coding (Database)
- Using PPA to install the lightweight theme software HotShots 2.1.0 under Ubuntu (Linux)
- Linux landing problem (Linux)
- Ubuntu 15.04 installation Powercommands 2.0 (Linux)
- Linux vi command list (Linux)
- CentOS 5.x install Lua 5.2.3 error (LIBS = -lncurses) (Linux)
- Linux tmux tcpdump summary (Linux)
- Linux Getting Started tutorial: Experience VirtualBox Virtual Machine chapter (Linux)
- Docker Basic and Advanced (Linux)
- Use mdadm tool to create software RAID 0 (Linux)
- Use source packages compiled and installed GCC5.1 in Mac OS X 10.10.3 (Linux)
- Java implementation of stacks and queues (Programming)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.