一、安装MinGW
http://blog.csdn.net/mecho/article/details/24305369
我的是64位的
安装我放在:
C:\mingw-w64\mingw64
环境变量:
二、安装OCI
三、配置go-oci8
直接go get github.com/wendal/go-oci8(报错不用管),然后到go\src\github.com/wendal\go-oci8\windows下,将pkg-config.exe拷贝到C:\mingw-w64\mingw64\bin下,将oci8.pc复制到C:\mingw-w64\mingw64\lib\pkg-config\下,并且编辑oci8.pc:
# Package Information for pkg-config
prefix=修改为instantclient_11_1目录,如C:/androidtools/orcale/instantclient_12_2
exec_prefix=修改为instantclient_11_1目录,如C:/androidtools/orcale/instantclient_12_2
libdir=${exec_prefix}
includedir=${prefix}/sdk/include/
Name: OCI
Description: Oracle database engine
Version: 11.2
Libs: -L${libdir} -loci
Libs.private:
Cflags: -I${includedir}
四、设置环境变量
环境变量path下添加C:/androidtools/orcale/instantclient_12_2和C:\mingw-w64\mingw64\bin的路径
添加PKG_CONFIG_PATH=C:\mingw-w64\mingw64\lib\pkg-config
添加TNS_ADMIN=C:/androidtools/orcale/instantclient_12_2\network\admin\
五、测试
go get github.com/mattn/go-oci8 执行不报错就对了 用这个驱动中文不乱码
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-oci8"
"log"
"os"
)
// func getDSN() string {
// var dsn string
// if len(os.Args) > 1 {
// dsn = os.Args[1]
// if dsn != "" {
// return dsn
// }
// }
// dsn = os.Getenv("GO_OCI8_CONNECT_STRING")
// if dsn != "" {
// return dsn
// }
// fmt.Fprintln(os.Stderr, `Please specifiy connection parameter in GO_OCI8_CONNECT_STRING environment variable,
// or as the first argument! (The format is user/[email protected]:port/sid)`)
// }
func main() {
// 为log添加短文件名,方便查看行数
log.SetFlags(log.Lshortfile | log.LstdFlags)
log.Println("Oracle Driver example")
//os.Setenv("NLS_LANG", "")
// 用户名/密码@IP:端口/实例名
db, err := sql.Open("oci8", )
if err != nil {
log.Fatal(err)
}
rows, err := db.Query("select 3.14, 'foo' from dual")
if err != nil {
log.Fatal(err)
}
defer db.Close()
for rows.Next() {
var f1 float64
var f2 string
rows.Scan(&f1, &f2)
log.Println(f1, f2) // 3.14 foo
}
rows.Close()
// 先删表,再建表
db.Exec("drop table sdata")
db.Exec("create table sdata(name varchar2(256))")
db.Exec("insert into sdata values('中文')")
db.Exec("insert into sdata values('[email protected]#$%^&*()_+')")
rows, err = db.Query("select * from sdata")
if err != nil {
log.Fatal(err)
}
for rows.Next() {
var name string
rows.Scan(&name)
log.Printf("Name = %s, len=%d", name, len(name))
}
rows.Close()
}
linux oci8
1,下载linux版OCI
解压到/usr/lib下
执行下面命令
ln /usr/lib/instantclient_12_2/libclntsh.so.12.1 /usr/lib/libclntsh.so
ln /usr/lib/instantclient_12_2/libocci.so.12.1 /usr/lib/libocci.so
ln /usr/lib/instantclient_12_2/libociei.so /usr/lib/libociei.so
ln /usr/lib/instantclient_12_2/libnnz12.so /usr/lib/libnnz12.so
安装pkg-config
在 /usr/lib/pkg-config 目录下创建文件 oci8.pc,内容如下:
# Package Information for pkg-config
prefix=/usr/lib/instantclient_12_2
exec_prefix=/usr/lib/instantclient_12_2
libdir=${exec_prefix}
includedir=${prefix}/sdk/include/
Name: OCI
Description: Oracle database engine
Version: 12.2
Libs: -L${libdir} -lclntsh
Libs.private:
Cflags: -I${includedir}
环境变量设置
vim /etc/profile
在最后加上
export ORACLE_HOME=/usr/lib/instantclient_12_2
export LD_LIBRARY_PATH=$ORACLE_HOME
export PKG_CONFIG_PATH=/usr/lib/pkg-config
source /etc/profile
执行驱动下载
go get github.com/mattn/go-oci8
mac
oci和oci sdk 下载地址:下载地址mac (下载instantclient-basic和instantclient-sdk)
1. 下载完成后解压
2. 文件-前往文件夹/usr/local 创建pkg-config文件夹
3. 文件夹下面创建oci8.pc文件
prefix=/Users/yhlyl/java/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}
4. 环境变量设置
vim .bash_profile
export ORACLE_HOME=/Users/yhlyl/java/instantclient_12_1
export LD_LIBRARY_PATH=$ORACLE_HOME
export PKG_CONFIG_PATH=/usr/local/pkg-config
source .bash_profile
go get github.com/mattn/go-oci8