本文参考了以下地址的文章:
https://github.com/Centny/Centny/blob/master/Articles/How%20build%20github.com%3amattn%3ago-oci8.md?
一、安装oracle的OCI套件
1、OCI下载链接页面下载(instantclient-basic,instantclient-sdk)
http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
2、解压缩到同一个目录下,比如:instantclient_12_1
3、root权限移动文件夹到目录 /usr/lib 下
2、root权限执行以下命
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
http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
2、解压缩到同一个目录下,比如:instantclient_12_1
3、root权限移动文件夹到目录 /usr/lib 下
2、root权限执行以下命
##? 其实直接cp拷贝过去也是一样的
ln /usr/lib/instantclient_12_1/libclntsh.so.12.1 /usr/lib/libclntsh.soln /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
## 以下两条是为了运行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路径加入系统加载动态库的路径中,并重新加载一次?
?echo /opt/oracle/instantclient >> /etc/ld.so.conf
?ldconfig
3、安装pkg-config
4、在 /usr/lib/pkgconfig 目录下创建文件 oci8.pc,内容如下:
prefix=<replace instantclient path> // 路径改为/usr/lib/instantclient_12_1
libdir=${prefix}
includedir=${prefix}/sdk/include/
Name: OCI
Description: Oracle database engine
Version: 12.1 // 版本改为实际的版本号
Libs: -L${libdir} -lclntsh
Libs.private:
Cflags: -I${includedir}
5、直接运行步骤6会报libaio不存在的错误,安装libaio库
sudo apt-get install libaio1
6、安装go-oci8
go get github.com/mattn/go-oci8?
7、.bashrc 文件中添加系统变量
# OCI安装目录?
export ORACLE_HOME=/usr/lib/instantclient_12_1
# ?tnsnames.ora 文件地址?
?export TNS_ADMIN=$ORACLE_HOME/network/admin
# OCI安装目录加入动态库加载路径?
?export LD_LIBRARY_PATH=$ORACLE_HOME
# oci8.pc文件所在路径
export PKG_CONFIG_PATH=/usr/lib/pkgconfig
8、 tnsnames.ora 文件的内容
?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)
)
)
?
二、使用github.com/mattn/go-oci8操作Oracle数据库?
?2.1、增删改查package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-oci8"
"os"
)
func main() {// 字符集os.Setenv("NLS_LANG", "AMERICAN_AMERICA.AL32UTF8")
// 注意连接字符串的写法
db, err := sql.Open("oci8", "awsdb/awsdb@192.168.0.126:1521/awsdb")
if err != nil {
fmt.Println(err)
return
}
// 事务开启
myTx,err:=db.Begin()
myTx.Commit()
// 查询与结果遍历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
}
// 关闭数据库连接
db.Close()
}2.2、执行了一个存储过程、使用结构保存结果package main
import (
"database/sql"
_ "github.com/mattn/go-oci8"
"fmt"
"os"
"sync"
)
var (
db *sql.DB
mux sync.Mutex
)
// 多行字符串的定义
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() {// 锁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=‘中国人‘ 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 // 这个地方改成string才不会报错,但是我创建数据库是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()
}
?
?